I have a huge XML response from an external end point. I want to parse the XML response to java classes. I was able to parse into respective POJOS if none of the XML had namespaces and things went well till that point.
However, the response might contain namespaces only at the root element. For eg, like this
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="somevalue here"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RestOfTheDocument> something here </RestOfTheDocument>
</Document>
I can guarantee that none of the inner xml elements will have any more namespaces.
Is there a way to implement this? I saw a few answers ignoring namespaces completely but dont feel very convincing. Is there a way to properly parse these.
This is my java class to model the XML response
@XmlRootElement(name = "Document")
static class Response {
@XmlElement(name = "RestOfTheDocument", required = true, nillable = false)
RestOfTheDocument restOfTheDocument;
}
what I tried ?
Add namespace information to the @XmlRootElement like this
@XmlRootElement(name = "Document", namespace = "somevalue here")
Doing so is making all the inner xml elements to be NULL.
NOTE: I have abstracted the huge inner level to be RestOfTheDocument but there are lot many but none of them will have any namespaces whatsoever in the response!
Thank you!
SimpleXml will ignore the namespaces and just parse the XML:
This will output:
From maven central: