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.