This is driving me crazy, I'm working on a project that requires me to parse XML documents (UBL format).
These documents might have one or more attachments in their body (base64 encoded) and it's my job to get them out. I also need to get some other fields out (some of which work), for brevity i'll rename namespaces to "A:", "B:", etc
Example xml (heavily simplified)
<Invoice>
<A:ID>*someID*</A:ID>
<B:AdditionalDocumentReference>
<A:ID>attachmentID</A:ID>
<B:OrderReference>
<A:ID>16009896</A:ID>
</B:OrderReference>
<A:DocumentType>PDF</A:DocumentType>
<B:Attachment>
<A:EmbeddedDocumentBinaryObject mimeCode="application/pdf">
*base64 encoded string*
</A:EmbeddedDocumentBinaryObject>
</B:Attachment>
</B:AdditionalDocumentReference>
</Invoice>
Problem 1: I can't assume that the root element will be named "Invoice".
To retrieve the attachments I use:
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList)xPath.evaluate("//AdditionalDocumentReference",
doc.getDocumentElement(), XPathConstants.NODESET);
This returns nothing, I have also tried:
.//AdditionalDocumentReference
And
//B:AdditionalDocumentReference
Neither of which works, the only thing that works is:
//Invoice/AdditionalDocumentReference
But as I said above the root element might be named differently so not really an option.
I have the same issue with getting the ID from my document. I thought that the easiest way would be to use:
//ID
I know the first occurrence of that ID tag is the ID of the document but this also returns nothing. It only works if I use:
//Invoice/ID
Now for the really weird part. See that order reference tag? I use this one-liner:
xPath.evaluate("//OrderReference/ID", document)
and it works...
What am i doing wrong?