Hello,<div><br></div><div><div class="gmail_quote">The method "contains" you use checks if the particular Java Element object exists in element list. It obviously doesn't.</div><div class="gmail_quote"><br></div>
<div class="gmail_quote">Use XPath with properly initialized namespaces and query</div><div class="gmail_quote"><br></div><div class="gmail_quote">//hasPart[text()='A1_4_001'] </div><div class="gmail_quote"><br></div>
<div class="gmail_quote">or similar.</div><div class="gmail_quote"><br></div><div class="gmail_quote">Regards,</div><div class="gmail_quote">Grzegorz </div><div class="gmail_quote"><br></div><div class="gmail_quote">2012/4/13 Lighton Phiri <span dir="ltr"><<a href="mailto:lighton.phiri@gmail.com">lighton.phiri@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Is there an easier way of checking if an element exists in an XML<br>
file? I tried to use the List function 'contains' to do my comparison,<br>
but it seems to fail; checked the Element class for appropriate<br>
methods, but nothing there. The closest I came to finding something<br>
similar on <a href="http://markmail.org" target="_blank">markmail.org</a> is an old article [1] that isn't quite<br>
helpful. The only workaround I can think of at the moment is a<br>
recursive walk-through to check the textual content of each element,<br>
but something tells me there has to be a built-in mechanism for doing<br>
this.<br>
<br>
Code snippet<br>
<br>
Element hasPartNode = new Element("hasPart",<br>
Namespace.getNamespace("dcterms", "<a href="http://purl.org/dc/terms/" target="_blank">http://purl.org/dc/terms/</a>"));<br>
hasPartNode.addContent("A1_4_001");<br>
<br>
// This prints false<br>
System.out.println(rootNode.getChildren("hasPart",<br>
Namespace.getNamespace("dcterms",<br>
"<a href="http://purl.org/dc/terms/" target="_blank">http://purl.org/dc/terms/</a>")).contains(hasPartNode));<br>
<br>
Input XML document<br>
<br>
<resource xmlns:dc="<a href="http://purl.org/dc/elements/1.1/" target="_blank">http://purl.org/dc/elements/1.1/</a>"<br>
xmlns:dcterms="<a href="http://purl.org/dc/terms/" target="_blank">http://purl.org/dc/terms/</a>"><br>
<dc:title>Notebooks</dc:title><br>
<dcterms:hasPart>A1_4_001</dcterms:hasPart><br>
<dcterms:hasPart>A1_4_002</dcterms:hasPart><br>
<dcterms:hasPart>A1_4_003</dcterms:hasPart><br>
</resource><br>
<br>
[1] <a href="http://markmail.org/message/s7le3js7r5vub7oy" target="_blank">http://markmail.org/message/s7le3js7r5vub7oy</a><br>
<br>
-- Phiri<br>
_______________________________________________<br>
To control your jdom-interest membership:<br>
<a href="http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com" target="_blank">http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com</a><br>
</blockquote></div><br></div>