users@jaxb.java.net

Re: Unmarshall and Marshalling does not produce the same XML

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Tue, 17 Mar 2015 17:21:35 +0100

Hi,

consider using XMLUnit for test cases to compare XML. Direct comparison
does not work as logically equivalent XML may very well be syntactically
different.
XMLUnit supports "similar" semantics.

Best wishes,
Alexey

On Tue, Mar 17, 2015 at 4:35 PM, Herpertz, Francesca <
francesca.herpertz_at_bearingpoint.com> wrote:

> Hi,
>
> I want to do it because the other system which later on should get the
> XML file does not handle namespaces.
>
> I know – technically you are doing everything correctly…. But I was
> hoping on some easy way to get the java object into the output format with
> jaxb.
>
> Kind regards,
> Francesca
>
>
>
> From: <Herpertz>, Francesca Herpertz <
> francesca.herpertz_at_bearingpoint.com>
> Date: Dienstag, 17. März 2015 14:19
> To: "users_at_jaxb.java.net" <users_at_jaxb.java.net>
> Subject: Unmarshall and Marshalling does not produce the same XML
>
> 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">
>
> <modulename="B_stat"f001Filter="qualifier&gt;=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:modulename="B_stat"f001Filter="qualifier&gt;=4 and (a is null or
> a=0)">
>
> …
> …
> …
>
> My java code looks like this:
>
> privatevoid 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.
>