Hi,
I got a problem with a small unit test I am trying to create.
My test unmarshals an existing XML into a java object (Generated by the XJC plugin via maven) and marshals it back into an XML.
In the end I am trying to compare both files with one another. Sadly those two files are not the same.
The issue I am having is that JAXB will add a default prefix for the namespace during the marshaling process.
My xml input looks the following:
<?xml version="1.0"?>
<validation xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xmlns="
http://somenamespace.com/ValidationType" xsi:noNamespaceSchemaLocation="
http://somenamespace.com/test/xsd/validation.xsd">
<module name="B_stat" f001Filter="qualifier>=4 and (a is null or a=0)">
...
...
...
What I get as a result looks the following:
<?xml version="1.0"?>
<ns2:validation xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="
http://somenamespace.com/ValidationType" xsi:noNamespaceSchemaLocation="
http://somenamespace.com/test/xsd/validation.xsd">
<ns2:module name="B_stat" f001Filter="qualifier>=4 and (a is null or a=0)">
...
...
...
My java code looks like this:
private void marshal(String filepath, Validation validation) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Validation.class);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "
http://somenamespace.com/test/xsd/validation.xsd");
XMLStreamWriter writer = (XMLStreamWriter) XMLOutputFactory
.newInstance().createXMLStreamWriter(new FileWriter(filepath));
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File("src/main/resources/xsd/validation.xsd"));
m.setSchema(schema);
m.marshal(validation, writer);
writer.flush();
writer.close();
}
How would I get rid of the ns2: prefix but keep my xmlns="
http://somenamespace.com/ValidationType" ? I have found several articles which suggest creating a custom mapper. Some also say that the only way to handle this is to traverse the DOM tree after its creation and remove the ns2: prefixes manually. Is there an elegant and by the framework supported way to get the same back what I initially unmashaled?
Please keep in mind that my Java classes are generated via maven and a change in them would not help me for long (until the next person makes a mvn package ;) )
I would really appreciate some help here.
Thanks and kind regards,
Francesca Herpertz
________________________________
BearingPoint Software Solutions GmbH
Geschäftsführer: Jürgen Lux, Thomas Sauer-Brabänder, Dr. Robert Wagner
Sitz: Frankfurt am Main
Registergericht: Amtsgericht Frankfurt am Main HRB 81430
The information in this email is confidential and may be legally privileged. If you are not the intended recipient of this message, any review, disclosure, copying, distribution, retention, or any action taken or omitted to be taken in reliance on it is prohibited and may be unlawful. If you are not the intended recipient, please reply to or forward a copy of this message to the sender and delete the message, any attachments, and any copies thereof from your system.