tags:

views:

253

answers:

2

I have different forms of XML that I must analyse,

<AMDI>
...
<MI>
</MI>
</AMDI>

or

<AMDI>
...
<AD>
</AD>
</AMDI>

So I want to build an XPath query depending of the type of the node (if it's an MI : XML_ITEMS = "//MI/DL/D", if it's an AD : XML_ITEMS = "//AD/DL/D") I'm working with DocumentBuilder and XPathExpression. Thnx for the help :)

A: 

You can also list the different nodes like this :

Node node = doc.getDocumentElement();
NodeList type = node.getChildNodes();
for(int i=0; i<nodes.getLength();i++)
     System.out.println(type.item(i).getNodeName();

And choose the write node :) Thanx a lot

taichimaro
+1  A: 

In XPath 1.0:

//MI/DL/D  |   //AD/DL/D

This is not syntactically legal in XPath 1.0:

//(MI|AD)/DL/D

But you may use it in case you have an XPath 2.0 engine.

Dimitre Novatchev
Thnx :) I should work more XPath, but just as a question, is XPath the best way to parse an XML Document ????
taichimaro
@taichimaro: XPath is the best way to *search* within an XML document. XPath works on XML documents that are already parsed by an XML parser.
Dimitre Novatchev
@Dimitre Novatchev : and if I want to build an XML document from scratch ?
taichimaro