Remove an XML Node if an attribute has a specific value





I have the need to search into an XML for some attributes and delete its Node if the attribute is found. For example, I want to remove the book nodes which have attributes beginning with "#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>


So far, I have been able to capture all "#false" Elements using an XPath expression:



String search = "//@*[starts-with(.,'#false')]";
NodeList nodeList = (NodeList) xPath.
compile(search).evaluate(doc, XPathConstants.NODESET);

((Node)nodeList.item(0)).getParent(); // NULL!!


However the problem is that the Parent Node of "available" Elements is null so I cannot find a way to remove the whole "book" Node. Any help ?


No comments:

Post a Comment