1

I have this XML:

<?xml version="1.0" encoding="UTF-8"?>
<s:XXMsg xmlns:s="com:sample:test:msg">
    <s:Header>
        <s:cpyCod>xxx</s:cpyCod>
        <s:appCod>yyy</s:appCod>
    </s:Header>
</s:XXMsg>

and I want this to be unmarshalled into the following Java class:

public class XXHeader
{
    private String cpyCod;
    private String appCod;
}

I have written this code:

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
    try
    {
        XMLStreamReader streamReader = inputFactory
            .createXMLStreamReader(Test.class.getClassLoader().getResourceAsStream("xxmsg.xml"));
        while (streamReader.hasNext())
        {
            int next = streamReader.next();
            if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT
                && streamReader.getLocalName().equals("Header"))
            {
                JAXBContext context = JAXBContext.newInstance(XXHeader.class);
                Unmarshaller unmarshaller = context.createUnmarshaller();
                XXHeader xxHeader = unmarshaller.unmarshal(streamReader, XXHeader.class).getValue();
                System.out.println(xxHeader);
                break;
            }
        }

I am getting all field values of XXHeader as null. But if I do change the xml and remove the prefixes s: in every element then I get the correct values.

How can I ignore this prefix while unmarshalling to get the correct values?

PS: Use of @XML** annotations in XXHeader class is discouraged.

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
  • Does this answer your question? [JAXB: How to ignore namespace during unmarshalling XML document?](https://stackoverflow.com/questions/277502/jaxb-how-to-ignore-namespace-during-unmarshalling-xml-document) – Vadzim Jun 02 '20 at 13:39
  • @Vadzim I had added the reference to [another answer](https://stackoverflow.com/a/24387115/725306) on the same question (https://stackoverflow.com/questions/277502/jaxb-how-to-ignore-namespace-during-unmarshalling-xml-document) in [my answer](https://stackoverflow.com/a/44431441/725306) below, so that certainly helped. – Mohammad Faisal Jun 03 '20 at 05:42

2 Answers2

1

I am able to do this using StreamReaderDelegate as mentioned here:

public class XXHeaderXMLReader
    extends StreamReaderDelegate
{

    public XXHeaderXMLReader(XMLStreamReader paramXMLStreamReader)
    {
        super(paramXMLStreamReader);
    }

    @Override
    public String getAttributeNamespace(int paramInt)
    {
        return "";
    }

    @Override
    public String getNamespaceURI()
    {
        return "";
    }

}

and adding the line in previous code:

XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
try
{
    XMLStreamReader streamReader = inputFactory
        .createXMLStreamReader(DocumentTest.class.getClassLoader().getResourceAsStream("xxmsg.xml"));
    while (streamReader.hasNext())
    {
        int next = streamReader.next();
        if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT)
        {
            if (streamReader.getLocalName().equals("Header"))
            {
                JAXBContext context = JAXBContext.newInstance(XXHeader.class);
                Unmarshaller unmarshaller = context.createUnmarshaller();
                XXHeaderXMLReader xxHeaderXMLReader = new XXHeaderXMLReader(streamReader);
                XXHeader xxHeader =
                    unmarshaller.unmarshal(xxHeaderXMLReader, XXHeader.class).getValue();
                System.out.println(xxHeader);
                break;
            }
        }
    }
}
catch (XMLStreamException e)
{
    e.printStackTrace();
}
catch (JAXBException e)
{
    e.printStackTrace();
}
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
0

Here you may find useful information about ignoring namespace or you may define XML schema (with field description) and use it instead of com:sample:test:msg.

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
Andriy Rymar
  • 313
  • 3
  • 8