如何解析XML

如何解析XML,第1张

如何解析XML
public static void main(String[] args) throws Exception {  File file = new File("data.xml");  documentBuilder builder = documentBuilderFactory.newInstance().newdocumentBuilder(); //if you are using this pre for blackberry xml parsing  builder.setCoalescing(true);  document doc = builder.parse(file);  NodeList nodes = doc.getElementsByTagName("topic");  for (int i = 0; i < nodes.getLength(); i++) {    Element element = (Element) nodes.item(i);    NodeList title = element.getElementsByTagName("title");    Element line = (Element) title.item(0);    System.out.println("Title: " + getCharacterDataFromElement(line));  }}public static String getCharacterDataFromElement(Element e) {  Node child = e.getFirstChild();  if (child instanceof CharacterData) {    CharacterData cd = (CharacterData) child;    return cd.getData();  }  return "";}

(http://www.java2s.com/Code/Java/XML/GetcharacterdataCDATAfromxmldocument.htm)



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4907165.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存