请问:dom4j怎么遍历某一个节点(节点是通过属性名指定)下的子节点

发布于 2021-11-13 15:29:41 字数 505 浏览 880 评论 4

<?xml version='1.0' encoding='utf-8'?>
<tableList>
 <table name="dd">
  <column key="d" value="avc">bac</column>
 </table>
<table name="AA">
  <column key="d" value="avc">bac</column>
</table>

<table name="cc">
  <column key="d" value="avc">bac</column>
</table>

</tableList>

通过table 的name属性遍历column子节点,先谢谢大家了,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

叹沉浮 2021-11-19 18:33:23

亲,这样试试~

String content = "xml文件字符串";
Document doc = DocumentHelper.parseText(content);
List tableList = doc.selectNodes("//tableList/table");
for (Object tableObj : tableList ) {
   Element table = (Element) tableObj;
   if(null != table){
      String tableName = table.attributeValue("name");
      List columnList = table.getDocument().selectNodes("//table/column");
      for(Object columnObj : columnList){
         Element column= (Element) tableObj;
         String key = column.attributeValue("key");
         String value = column.attributeValue("value");
         String text = column.getTextTrim();
      )
   }
}
嘦怹 2021-11-18 15:41:37

最简单的办法 

SAXReader reader = new SAXReader();
		Element root = reader.read("fileName.xml").getDocument()
				.getRootElement();
		List<Element> nodes = root.elements("table");
		for (Element node : nodes) {
			if ("xxxx".equals(node.attributeValue("name"))) {
				Element column = node.element("column");
				//some code
			}
		}

另外 你还可以用xpath的方式来解决

水水月牙 2021-11-18 14:46:41

亲,这样试试~

String content = "xml文件字符串";
Document doc = DocumentHelper.parseText(content);
List tableList = doc.selectNodes("//tableList/table");
for (Object tableObj : tableList ) {
   Element table = (Element) tableObj;
   if(null != table){
      String tableName = table.attributeValue("name");
      List columnList = table.getDocument().selectNodes("//table/column");
      for(Object columnObj : columnList){
         Element column= (Element) tableObj;
         String key = column.attributeValue("key");
         String value = column.attributeValue("value");
         String text = column.getTextTrim();
      )
   }
}
别再吹冷风 2021-11-15 03:54:34

最简单的办法 

SAXReader reader = new SAXReader();
		Element root = reader.read("fileName.xml").getDocument()
				.getRootElement();
		List<Element> nodes = root.elements("table");
		for (Element node : nodes) {
			if ("xxxx".equals(node.attributeValue("name"))) {
				Element column = node.element("column");
				//some code
			}
		}

另外 你还可以用xpath的方式来解决

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文