Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dom4j not parsing xml file #170

Open
billikill opened this issue Oct 19, 2023 · 6 comments
Open

dom4j not parsing xml file #170

billikill opened this issue Oct 19, 2023 · 6 comments
Labels

Comments

@billikill
Copy link

Hello,
I have an xml file and I can't parse it. Could you please help with getting the data from the file.

String xml = """
<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope//' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<soap:Body>
 <GetObjectListResponse xmlns='http://www.omninet.de/OtWebSvc/v1'>
     <GetObjectListResult success='true' errorMsg='' totalNumberResults='296'>
   <Object id="55419192">
             <LongIntVal name="Number">94893</LongIntVal>
            </Object>
            <Object id="55419193">
             <LongIntVal name="Number">94899</LongIntVal>
            </Object>
  </GetObjectListResult>
 </GetObjectListResponse>
</soap:Body>
</soap:Envelope>"""

Document doc = DocumentHelper.parseText(xml)
List<Node> nodes = doc.selectNodes("//Object")
@billikill
Copy link
Author

изображение

@ecki
Copy link

ecki commented Oct 19, 2023

Do you get any error, wrong results or no result? YoUr sample code and the screenshot seems different, (in your sample you use ‘ instead of “ and define a default namespace). maybe provide a link to raw test data?

if I had to guess, you need to turn off namespaces or filter ignoring the namespace with //*[local-name(.)='Object'], but that does not sound parsing related. May start with doc.toString() and .getChildren()?

@billikill
Copy link
Author

Do you get any error, wrong results or no result?
Hi, I'm not getting any data with no errors

@billikill
Copy link
Author

[//*[local-name(.)='Object']](url) its it working.
doc.toString() - > org.dom4j.tree.DefaultDocument@c9620d6 [Document: name null]

@ecki
Copy link

ecki commented Oct 22, 2023

Yeah so this means parsing worked, and it’s probably the namespace support. Your sample data does not define a namespace prefix, but I think you can define them yourself //ns1:Object would work then. (I am not sure if you can specify absolute namespace like//{http://www.omninet.de/…}Object instead of prefix)

@Boris-de
Copy link

Boris-de commented Nov 1, 2023

To do what ecki suggested, try to create an XPath object with the namespaces you need. Like this

DocumentFactory documentFactory = new DocumentFactory();
documentFactory.setXPathNamespaceURIs(Map.of("v1", "http://www.omninet.de/OtWebSvc/v1"));
XPath xpath = documentFactory.createXPath("//v1:Object");
List<Node> nodes = xpath.selectNodes(doc);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants