How to set namespace to false?

I am trying to parse some XML with EclipseLink MOXy and it does not work on a string with the xsi attribute. If I remove this, it is well versed. However, I have 100GiB XML to go through and modify the source files is not an option.

It has been suggested that if I can install XmlParser.setNamespaceAware(false) then it should work, but I have no idea how to configure this without breaking directly into MOXy's guts.

 <record> <header> <!-- citation-id: 14404534; type: journal_article; --> <identifier>info:doi/10.1007/s10973-004-0435-2</identifier> <datestamp>2009-04-28</datestamp> <setSpec>J</setSpec> <setSpec>J:1007</setSpec> <setSpec>J:1007:2777</setSpec> </header> <metadata> <crossref xmlns="http://www.crossref.org/xschema/1.0" xsi:schemaLocation="http://www.crossref.org/xschema/1.0 http://www.crossref.org/schema/unixref1.0.xsd"> <journal> <journal_metadata language="en"> [...] 

The exception that I get when the xsi: prefix xsi: present:

 org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException - with linked exception: [Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.XMLMarshalException Exception Description: An error occurred unmarshalling the document Internal Exception: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[13,107] Message: http://www.w3.org/TR/1999/REC-xml-names-19990114#AttributePrefixUnbound?crossref&xsi:schemaLocation&xsi] 
+6
source share
2 answers

There is currently no EclipseLink JAXB (MOXy) option to ignore namespaces. But there is an approach that you can use using the StAX parser.

Demo

You can create a StAX XMLStreamReader on an XML input that is not a namespace, and then disable MOXy.

 package forum13416681; import javax.xml.bind.*; import javax.xml.stream.*; import javax.xml.transform.stream.StreamSource; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Foo.class); XMLInputFactory xif = XMLInputFactory.newFactory(); xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false); StreamSource source = new StreamSource("src/forum13416681/input.xml"); XMLStreamReader xsr = xif.createXMLStreamReader(source); Unmarshaller unmarshaller = jc.createUnmarshaller(); Foo root = (Foo) unmarshaller.unmarshal(xsr); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(root, System.out); } } 

Java Model (Foo)

 package forum13416681; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Foo { private String bar; public String getBar() { return bar; } public void setBar(String bar) { this.bar = bar; } } 

Input (input.xml)

The following is a simplified version of XML from your question. Note: this XML is not a proper namespace because it lacks a namespace declaration for the xsi prefix.

 <?xml version="1.0" encoding="UTF-8"?> <foo xsi:schemaLocation="http://www.crossref.org/xschema/1.0 http://www.crossref.org/schema/unixref1.0.xsd"> <bar>Hello World</bar> </foo> 

Exit

The following is the result of running the demo code.

 <?xml version="1.0" encoding="UTF-8"?> <foo> <bar>Hello World</bar> </foo> 
+8
source

Instead of completely disabling namespace understanding, you can use a mechanism specific to the StAX implementation to pre-declare the xsi prefix, then parse with the included namespaces. For example, Woodstox you can say:

 import javax.xml.bind.*; import javax.xml.stream.*; import javax.xml.transform.stream.StreamSource; import com.ctc.wstx.sr.BasicStreamReader; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance("com.example"); XMLInputFactory xif = XMLInputFactory.newFactory(); StreamSource source = new StreamSource("input.xml"); XMLStreamReader xsr = xif.createXMLStreamReader(source); ((BasicStreamReader)xsr).getInputElementStack().addNsBinding( "xsi", "http://www.w3.org/2001/XMLSchema-instance"); 

and then create unmarshaller and cancel the xsr command, as in the Blaise message . Although this obviously connects you to one specific StAX implementation, it means that you do not need to modify existing JAXB model classes if they expect the <crossref> element and its children to be in the http://www.crossref.org/xschema/1.0 namespace http://www.crossref.org/xschema/1.0 .

+2
source

All Articles