I need to figure out a way to deal with the following.
Need to process a XML which is provided by different vendors the schema and structure of these xmls from different vendors are the same. the only difference is in the @XmlRootElement.
This is the name space i originally had
When I processed the XML file from the second vendor, their namespace was as below
and this caused the following exception
I was looking around for a solution, that would help me to remove the namespace from getting generated unmarshalling
Came across the following
http://stackoverflow.com/questions/7184526/jaxb-how-can-i-unmarshal-xml-without-namespaces/7206666
This post was helpful, so this is what i did to solve the problem
1. Removed the namespace entry from @XmlRootElement
Need to process a XML which is provided by different vendors the schema and structure of these xmls from different vendors are the same. the only difference is in the @XmlRootElement.
This is the name space i originally had
@XmlRootElement(name = "opsReports", namespace= "http://www.witsml.org/schemas/1series")
When I processed the XML file from the second vendor, their namespace was as below
xmlns="http://www.witsml.org/schemas/131"
and this caused the following exception
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.witsml.org/schemas/131", local:"opsReports"). Expected elements are <{http://www.witsml.org/schemas/1series}opsReports>.
I was looking around for a solution, that would help me to remove the namespace from getting generated unmarshalling
Came across the following
http://stackoverflow.com/questions/7184526/jaxb-how-can-i-unmarshal-xml-without-namespaces/7206666
This post was helpful, so this is what i did to solve the problem
1. Removed the namespace entry from @XmlRootElement
@XmlRootElement(name = "opsReports")
import java.io.FileInputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.util.StreamReaderDelegate;
public class Demo {
public static void main(String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream("\\WITSML_opsReport 20160622-172911.xml"));
xsr = new XsiTypeReader(xsr);
JAXBContext jc = JAXBContext.newInstance(OpsReportsJaxb.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
OpsReportsJaxb spec = (OpsReportsJaxb) unmarshaller.unmarshal(xsr);
System.out.println(spec);
}
private static class XsiTypeReader extends StreamReaderDelegate {
public XsiTypeReader(XMLStreamReader reader) {
super(reader);
}
@Override
public String getNamespaceURI() {
return "";
}
}
}
Hope this helps :))
No comments:
Post a Comment