Android:XML解析问题
我可以使用以下方法在此文件中读取和显示xmlAndroid:XML解析问题,android,xml,parsing,Android,Xml,Parsing,我可以使用以下方法在此文件中读取和显示xml private void loadTopMoversData() { ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); try { String xml = XMLfunctions.getXML(TOPMOVERS_XML); Doc
private void loadTopMoversData() {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
try {
String xml = XMLfunctions.getXML(TOPMOVERS_XML);
Document doc = XMLfunctions.XMLfromString(xml);
NodeList nodes = doc.getElementsByTagName("scrip");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("name", XMLfunctions.getValue(e, "name"));
map.put("currprice", XMLfunctions.getValue(e, "currprice"));
map.put("percentage", XMLfunctions.getValue(e, "percentage"));
map.put("code", XMLfunctions.getValue(e, "code"));
mylist.add(map);
Log.i("tag", XMLfunctions.getValue(e, "headline") + " " + XMLfunctions.getValue(e, "time") );
}
}
但是我无法使用相同的方法,但使用适当的标记来解析其中的xml。我做错了什么
private void loadNewsData() {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
try {
String xml = XMLfunctions.getXML(NEWS_XML);
Document doc = XMLfunctions.XMLfromString(xml);
NodeList nodes = doc.getElementsByTagName("data");
ArrayList<String> links = new ArrayList<String>();
for (int i = 0; i < nodes.getLength(); i++) {
Element e = (Element)nodes.item(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("headline", XMLfunctions.getValue(e, "headline"));
map.put("link", XMLfunctions.getValue(e, "link"));
map.put("time", XMLfunctions.getValue(e, "time"));
links.add(XMLfunctions.getValue(e, "link"));
mylist.add(map);
Log.i("tag", XMLfunctions.getValue(e, "headline") + " " + XMLfunctions.getValue(e, "time") );
}
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}`
这是我的XmlFunctions.java
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;
}
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 webURL){
String line = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(webURL);
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));
}
}
这与xml文件有关吗?请帮忙。谢谢
编辑:
还有谁能告诉我如何使用此解析器解析包含“&”的XML吗?您可能需要重新检查XML节点名称,区分大小写。还要检查您的链接数组是否正常工作 我解决了这个问题。在XMLfunctions.java中,它检查该节点是否是文本节点,并且xml中的节点显然不是文本节点
if( kid.getNodeType() == Node.TEXT_NODE ){
return kid.getNodeValue();
}
所以,我删除了这个条件,现在它可以正常工作了。谢谢第二个提要不是正确的XML。正如你所想&这不是正确的逃脱。它必须是这样的:&;见和 但是,一些解析器可以容忍这种情况,并且不会出错。你的解析器不是一个,如果这些
您可以尝试几个不同的解析器,也可以尝试在开始解析之前通过查找替换来修复提要。如果您这样做,您必须小心不要重新转义已转义的字符,否则您将得到如下结果:&;amp 解析第二个文档时会出现什么错误?我可以对解析XML的方式说eww吗?我可以建议您使用基于注释的解析器,如Simple。我甚至在这里写了一篇关于如何使用它的博客: