我正在制作一个练习应用程序,目的是从 RSS 提要中读取数据。
到目前为止一切顺利,除了我的应用程序遇到了特殊字符的问题。它读取节点内的第一个特殊字符,然后移动到下一个节点。
任何帮助将不胜感激,并对随后的大代码块感到抱歉。
RSS 提要 - www.usu.co.nz/usu-news/rss.xml
<title>Unitec hosts American film students</title>
<link>http://www.usu.co.nz/node/4640</link>
<description><p>If you’ve been hearing American accents around the Mt Albert campus over the past week.</description>
显示代码
String xml = XMLFunctions.getXML();
Document doc = XMLFunctions.XMLfromString(xml);
NodeList nodes = doc.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++)
{
Element e = (Element)nodes.item(i);
Log.v("XMLTest", XMLFunctions.getValue(e, "title"));
Log.v("XMLTest", XMLFunctions.getValue(e, "link"));
Log.v("XMLTest", XMLFunctions.getValue(e, "description"));
Log.v("XMLTest", XMLFunctions.getValue(e, "pubDate"));
Log.v("XMLTest", XMLFunctions.getValue(e, "dc:creator"));
}
读者代码
public class XMLFunctions
{
public final static Document XMLfromString(String xml)
{
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
System.out.println("XML parse error: " + e.getMessage());
return null;
} catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
} catch (IOException e) {
System.out.println("I/O exeption: " + e.getMessage());
return null;
}
return doc;
}
/** Returns element value
* @param elem element (it is XML tag)
* @return Element value otherwise empty String
*/
public final static String getElementValue( Node elem ) {
Node kid;
if(elem != null)
{
if (elem.hasChildNodes())
{
for(kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling())
{
if( kid.getNodeType() == Node.TEXT_NODE )
{
return kid.getNodeValue();
}
}
}
}
return "";
}
public static String getXML(){
String line = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.usu.co.nz/usu-news/rss.xml");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (MalformedURLException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (IOException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
}
return line;
}
public static int numResults(Document doc){
Node results = doc.getDocumentElement();
int res = -1;
try{
res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
}catch(Exception e ){
res = -1;
}
return res;
}
public static String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return XMLFunctions.getElementValue(n.item(0));
}
}
输出
Unitec hosts American film students
http://www.usu.co.nz/node/4640
<
Wed, 01 Aug 2012 05:43:22 +0000
Phillipa