0

My XML look like this

 <NMFIISERVICES>
        <service_status diffgr:id="service_status1" msdata:rowOrder="0" 
            diffgr:hasChanges="inserted">
            <service_return_code>0</service_return_code>
            <service_msg>Success</service_msg>
        </service_status>
 </NMFIISERVICES>

And it is giving the following error

The prefix "diffgr" for attribute "diffgr:id" associated with an element type "service_response" is not bound.

I need xml in this format

 <NMFIISERVICES>
        <service_status>
            <service_return_code>0</service_return_code>
            <service_msg>Success</service_msg>
        </service_status>
 </NMFIISERVICES>

How can I ignore the above diffgr attributes while unmarshalling in JAXB

2 Answers2

0

If you don't map the attributes, JAXB will automatically ignore it.

Your exception come from the XML Parser that JAXB uses, because your namespaces are not declared, your XML should be :

<NMFIISERVICES xmlns:diffgr="http://some.namespace" xmlns:msdata="http://some.other.namespace">
    <service_status diffgr:id="service_status1" msdata:rowOrder="0" 
        diffgr:hasChanges="inserted">
        <service_return_code>0</service_return_code>
        <service_msg>Success</service_msg>
    </service_status>
</NMFIISERVICES>

If you can't change your XML, you will need to parse it to remove the attributes namespace before unmarshalling it.

Dimpre Jean-Sébastien
  • 1,067
  • 1
  • 6
  • 14
0

You can use a solution like below

class XMLReaderWithoutNamespace extends StreamReaderDelegate {
    public XMLReaderWithoutNamespace(XMLStreamReader reader) {
      super(reader);
    }
    @Override
    public String getAttributeNamespace(int arg0) {
      return "";
    }
    @Override
    public String getNamespaceURI() {
      return "";
    }
}

InputStream is = new FileInputStream(filename);
XMLStreamReader xsr = XMLInputFactory.newFactory().createXMLStreamReader(is);
XMLReaderWithoutNamespace xr = new XMLReaderWithoutNamespace(xsr);
Unmarshaller um = jc.createUnmarshaller();
Object res = um.unmarshal(xr);

It will allow you to replace your diffgr namespace with an empty string

See here

Mike Adamenko
  • 2,944
  • 1
  • 15
  • 28