<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Hello Grzegorz,<br>
<br>
Thank you.<br>
<pre class="moz-signature" cols="72">Lighton Phiri
<a class="moz-txt-link-freetext" href="http://lightonphiri.org">http://lightonphiri.org</a>
<a class="moz-txt-link-freetext" href="http://en.gravatar.com/lightonphiri">http://en.gravatar.com/lightonphiri</a>
Twitter: @lightonphiri</pre>
<br>
On 13/04/2012 11:08, Grzegorz wrote:
<blockquote
cite="mid:CA+DoOidgCBgGR-p3CVMMxbRgX5JV_b6UxmUJChDAm9Yki9Cm+A@mail.gmail.com"
type="cite">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 moz-do-not-send="true"
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 moz-do-not-send="true"
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 moz-do-not-send="true"
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 moz-do-not-send="true" 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 moz-do-not-send="true"
href="http://purl.org/dc/elements/1.1/" target="_blank">http://purl.org/dc/elements/1.1/</a>"<br>
xmlns:dcterms="<a moz-do-not-send="true"
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 moz-do-not-send="true"
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 moz-do-not-send="true"
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>
</blockquote>
</body>
</html>