Reputation: 103
I'm getting some text from an xml file
URL url_Twitter = new URL("http://twitter.com/statuses/user_timelineID_PROVA.rss");
HttpURLConnection conn_Twitter =(HttpURLConnection)url_Twitter.openConnection();
DocumentBuilderFactory documentBF_Twitter = DocumentBuilderFactory.newInstance();
DocumentBuilder documentB_Twitter = documentBF_Twitter.newDocumentBuilder();
Document document_Twitter = documentB_Twitter.parse( conn_Twitter.getInputStream());
in the xml there are some characters like &# 8217; so when I call
document_Twitter.getElementsByTagName("title").item(2).getFirstChild().getNodeValue()
the string are trunked before that kind of characters
The text is in just one tag
<item>
<title>SMWRME: Internet per “Collaborare senza confini”. Soprattutto alla SMW di Roma, dal 7 all'11 febbraio. Ecco il terzo percorso. http://cot.ag/ewnJ4F</title>
<description>SMWRME: Internet per “Collaborare senza confini”. Soprattutto alla SMW di Roma, dal 7 all'11 febbraio. Ecco il terzo percorso. http://cot.ag/ewnJ4F</description>
<pubDate>Mon, 27 Dec 2010 20:05:01 +0000</pubDate>
<guid>http://twitter.com/SMWRME/statuses/19483914259140609</guid>
<link>http://twitter.com/SMWRME/statuses/19483914259140609</link>
<twitter:source><a href="http://cotweet.com/?utm_source=sp1" rel="nofollow">CoTweet</a></twitter:source>
<twitter:place/>
</item>
I noticed that this behavior does happen just for android application. The same code works fine for a java application. Can someone help me?
Upvotes: 3
Views: 582
Reputation: 34014
Can you try document_Twitter.getElementsByTagName("title").item(2).getTextContent()
instead? There might actually be multiple text nodes beneath this node, like
- "item" element
- "title" element
- text node "SMWRME: Internet per "
- text node "“"
- text node "Collaborare senza confini"
- text node "”"
Most SAX parsers would deliver the character content split in multiple parts so I can imagine a DOM parser to do the same. The method getTextContent should return the text content of all sub sub nodes concatenated.
You could also try to call setCoalescing(true) on your DocumentBuilderFactory before creating the DocumentBuilder, the documentation mentions that this affects CDATA sections but it might also change the handling of character entities.
Upvotes: 1