Necronet
Necronet

Reputation: 6813

Check whether XML tag exist in Java

I am parsing an XML javax.xml, but i want to know whether a tag exist on a child node For example

<tag>
 <child>
  <special_tag>Special</special_tag>
  <normal_tag>Normal</normal_tag>
 </child>
 <child>
  <normal_tag>Normal</normal_tag>
 </child>
</tag>

I am trying to know which Child has Special tag and which doesnt

Upvotes: 3

Views: 1061

Answers (3)

Boris Pavlović
Boris Pavlović

Reputation: 64650

Using xPath:

//child[special_tag]

Upvotes: 1

AlexR
AlexR

Reputation: 115398

It seems that this is a classic usecase for SAX. You should register your listener and check the tag name. When it equals to special_tag get the element's parent.

Upvotes: 0

Aravind Yarram
Aravind Yarram

Reputation: 80192

Build the DOM for your xml and use XPath to query for the existence of "special_tag" using //special_tag

Upvotes: 0

Related Questions