Re: Element comparison in XML document
Lighton Phiri <[email protected]> Fri, 13 Apr 2012 11:26:41 +0200
| Newsgroups | gmane.comp.java.jdom.general |
|---|---|
| Organization | lightonphiri.org |
| Message-ID | <[email protected]> |
Hello Grzegorz, Thank you. Lighton Phiri http://lightonphiri.org http://en.gravatar.com/lightonphiri Twitter: @lightonphiri On 13/04/2012 11:08, Grzegorz wrote: > Hello, > > The method "contains" you use checks if the particular Java Element > object exists in element list. It obviously doesn't. > > Use XPath with properly initialized namespaces and query > > //hasPart[text()='A1_4_001'] > > or similar. > > Regards, > Grzegorz > > 2012/4/13 Lighton Phiri <[email protected] > <mailto:[email protected]>> > > Is there an easier way of checking if an element exists in an XML > file? I tried to use the List function 'contains' to do my comparison, > but it seems to fail; checked the Element class for appropriate > methods, but nothing there. The closest I came to finding something > similar on markmail.org <http://markmail.org> is an old article > [1] that isn't quite > helpful. The only workaround I can think of at the moment is a > recursive walk-through to check the textual content of each element, > but something tells me there has to be a built-in mechanism for doing > this. > > Code snippet > > Element hasPartNode = new Element("hasPart", > Namespace.getNamespace("dcterms", "http://purl.org/dc/terms/")); > hasPartNode.addContent("A1_4_001"); > > // This prints false > System.out.println(rootNode.getChildren("hasPart", > Namespace.getNamespace("dcterms", > "http://purl.org/dc/terms/")).contains(hasPartNode)); > > Input XML document > > <resource xmlns:dc="http://purl.org/dc/elements/1.1/" > xmlns:dcterms="http://purl.org/dc/terms/"> > <dc:title>Notebooks</dc:title> > <dcterms:hasPart>A1_4_001</dcterms:hasPart> > <dcterms:hasPart>A1_4_002</dcterms:hasPart> > <dcterms:hasPart>A1_4_003</dcterms:hasPart> > </resource> > > [1] http://markmail.org/message/s7le3js7r5vub7oy > > -- Phiri > _______________________________________________ > To control your jdom-interest membership: > http://www.jdom.org/mailman/options/jdom-interest/[email protected] > > _______________________________________________ To control your jdom-interest membership: http://www.jdom.org/mailman/options/jdom-interest/[email protected]