这是我的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<productlist>
<product>
<title>1</title>
<price>30</price>
</product>
<product>
<title>2</title>
<price>9</price>
</product>
<product>
<title>3</title>
</product>
</productlist>
我需要做的是使用 Java DOM API 从包含“价格”标签的 XML 文件中打印出所有内容。像这样的事情
title:1
price:30
title:2
price:9
在 org.w3c.dom 文档中,我只找到 hasAttribute(String name) 方法来检查此元素是否具有属性,但我在文档中找不到像“hasTag(String name)”这样的方法。我找到这个网站http://www.java-forums.org/xml/62136-check-whether-xml-tag-exists-while-parsing-xml-using-dom-java.html但不幸的是我无法打开该网站。希望你能帮上忙。
最佳答案
只需调用 getElementsByTagName
并查看返回的列表是否有任何节点(使用 NodeList.getLength()
)。
关于Java DOM 如何检查节点是否存在于 XML 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15816593/