【发布时间】:2014-11-14 12:47:15
【问题描述】:
我需要在 XML 中搜索某些属性,如果找到该属性,则删除其节点。例如,我想删除属性以“#false”开头的书节点
<catalog>
<book id="bk101" available="#false">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>
到目前为止,我已经能够使用 XPath 表达式捕获所有“#false”元素:
String search = "//@*[starts-with(.,'#false')]";
NodeList nodeList = (NodeList) xPath.
compile(search).evaluate(doc, XPathConstants.NODESET);
((Node)nodeList.item(0)).getParent(); // NULL!!
但是问题是“可用”元素的父节点为空,所以我找不到删除整个“书”节点的方法。有什么帮助吗?
【问题讨论】: