0

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 :)

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Above you write "nodes.getLength()", do you mean "type.getLength()"?
2

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.

2 Comments

Thnx :) I should work more XPath, but just as a question, is XPath the best way to parse an XML Document ????
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.