dom4j 如何获取某节点下的所有节点

tinren 2008-08-07 04:04:03
<Records Version="0.1">
<Record busiId="0807291252000000100001" companyId="001">
<CustomerId>axiu45555</CustomerId>
<Content>
<Goods>
<Name>西湖牛肉羹</Name>
</Goods>
<Goods>
<Name>鱼香肉丝</Name>
</Goods>
</Content>
</Record>
<Record busiId="0807291252000000100002" companyId="002">
<CustomerId>geng888</CustomerId>>
<Content>
<Goods>
<Name>剁椒鱼头</Name>
</Goods>
</Content>
</Record>
</Records>

部分代码如下:
List list = document.selectNodes("//Record");

Iterator iter = list.iterator();

while (iter.hasNext()) {

Node node = (Node) iter.next();
System.out.println(node.valueOf("@busiId") + "="
+ node.valueOf("@companyId"));
List list2 = node.selectNodes("//Record/Content/Goods/*");
Iterator iter2 = list2.iterator();
while (iter2.hasNext()) {
Node node2 = (Node) iter2.next();
System.out.println(node2.getName() + "=" + node2.getText());

}
}

得到的结果如下:
0807291252000000100001=001
Name=西湖牛肉羹
Name=鱼香肉丝
Name=剁椒鱼头
0807291252000000100002=002
Name=西湖牛肉羹
Name=鱼香肉丝
Name=剁椒鱼头
=========================================================================
如何才能得到如下结果:

0807291252000000100001=001
Name=西湖牛肉羹
Name=鱼香肉丝
0807291252000000100002=002
Name=剁椒鱼头

哪位可以指点下,多谢了。
...全文
847 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tinren 2008-08-18
  • 打赏
  • 举报
回复
谢谢!!!
bootupnow 2008-08-08
  • 打赏
  • 举报
回复
偶没用xpath来写,改天也去学习下xpath


<?xml version="1.0" encoding="GBK"?>
<Records Version="0.1">
<Record busiId="0807291252000000100001" companyId="001">
<CustomerId>axiu45555 </CustomerId>
<Content>
<Goods>
<Name>西湖牛肉羹 </Name>
</Goods>
<Goods>
<Name>鱼香肉丝 </Name>
</Goods>
</Content>
</Record>
<Record busiId="0807291252000000100002" companyId="002">
<CustomerId>geng888 </CustomerId>>
<Content>
<Goods>
<Name>剁椒鱼头 </Name>
</Goods>
</Content>
</Record>
<Record busiId="0807291252000000100003" companyId="003">
<CustomerId>xfsss </CustomerId>>
<Content>
<Goods>
<Name>麻婆豆腐 </Name>
</Goods>
<Goods>
<Name>酸菜鱼</Name>
</Goods>
<Goods>
<Name>宫保鸡丁 </Name>
</Goods>
</Content>
</Record>
</Records>



import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Test {

public static void doSomething() throws DocumentException {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("C:/a.xml"));
Element rootElement = document.getRootElement();
System.out.println(rootElement.getName());
List<Element> listRecord = rootElement.elements("Record");
Iterator<Element> itRecord = listRecord.iterator();
while(itRecord.hasNext()){
Element e = itRecord.next();
System.out.println(e.attributeValue("busiId") + "=" + e.attributeValue("companyId"));
List<Element> listGoods = e.element("Content").elements("Goods");
Iterator<Element> itGoods = listGoods.iterator();
while(itGoods.hasNext()){
Element e2 = itGoods.next().element("Name");
System.out.println(e2.getName() + "=" + e2.getTextTrim());
}
}
}

public static void main(String[] args) {
try {
doSomething();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}




0807291252000000100001=001
Name=西湖牛肉羹
Name=鱼香肉丝
0807291252000000100002=002
Name=剁椒鱼头
0807291252000000100003=003
Name=麻婆豆腐
Name=酸菜鱼
Name=宫保鸡丁

天外流星 2008-08-08
  • 打赏
  • 举报
回复
List list2 = objDoc.selectNodes("Record/Content/Goods/*");
天外流星 2008-08-08
  • 打赏
  • 举报
回复
List list2 = node.selectNodes("//Record/Content/Goods/*");
taaking 2008-08-08
  • 打赏
  • 举报
回复
xiexie
laorer 2008-08-08
  • 打赏
  • 举报
回复
List list2 = node.selectNodes("//Record/Content/Goods/*");

你这个又是从根节点查询了,
List list2 = node.selectNodes("/Record/Content/Goods/*");
或者
List list2 = node.selectNodes("Record/Content/Goods/*");
试下

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧