tags:

views:

28

answers:

2

Is there a way to have a single implementation in Java that can read/parse an XML file with normal elements and another with prefixed elements? Both versions have the same structure.

i.e.

XML-1

<root>
  <element attribute="value">
  </element>
</root>

XML-2

<pre:root xmlns:pre="someURL">
  <pre:element attribute="value">
  </pre:element>
</pre:root>
A: 

This should be possible. When parsing using DOM/SAX/StAX you will need to account for the fact that sometimes the unqualified element name will correspond to what the parser considers the "qualified name" and sometimes it will correspond to the "local name".

Link to similar issue related to executing XPath statements:

Blaise Doughan
A: 

Using DOM, invoke the following:

DocumentBuilderFactory.setNamespaceAware(true);

Then use the wildcard when searching for tags:

Document.getElementsByTagNameNS("*","elementName");
Aleks Felipe