dom4j解析xml

来源:互联网 发布:充值软件是几折 编辑:程序博客网 时间:2024/06/08 04:17

文件 b.xml:
<RESULT>
 <VALUE id="13" type="1">
  <NO>2</NO>
  <ADDR>测试</ADDR>
 </VALUE>
 <VALUE id="13" type="2">
  <NO>2</NO>
  <ADDR>测试</ADDR>
 </VALUE>
</RESULT>

解析如下:
document = sax.read("b.xml");
 root = document.getRootElement();
 List ls = root.selectNodes("/RESULT/VALUE");
 for (int i = 0; i < ls.size(); i++) {
  Element val = (Element) ls.get(i);
  Element no = val.element("NO");
  Element addr = val.element("ADDR");
  System.out.println("id=" + val.attributeValue("id") + "\t type="
    + val.attributeValue("type") + "\t no=" + no.getText()
    + "\t addr=" + addr.getText());
 }

原创粉丝点击