Unmarshall and Marshalling does not produce the same XML

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Unmarshall and Marshalling does not produce the same XML

Herpertz, Francesca
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&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:module name="B_stat" f001Filter="qualifier&gt;=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.
Reply | Threaded
Open this post in threaded view
|

Re: Unmarshall and Marshalling does not produce the same XML

Mirko Klemm
Hi Francesca,
The infoset of both XML files is actually identical. Is there a specific reason why you would like to set the namespace prefixes to a specific value? Standard-conforming XML software should not depend on namespace prefixes in a document.
Regards,
Mirko

> Am 2015-03-17 um 14:19 schrieb Herpertz, Francesca <[hidden email]>:
>
> 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&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:module name="B_stat" f001Filter="qualifier&gt;=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.


smime.p7s (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Unmarshall and Marshalling does not produce the same XML

Wolfgang Laun-2
How do you compare?

A text file comparison will not only show a difference depending on those prefixes - it may also stumble due to the indeterminate order of attributes.

If the prefix is indeed the only difference you may try a custom mapper.

Another simple alternative for the unit test would be a String.replace or String.replaceAll removing the easily identifyable ":ns2" and "ns2:".

Also, /usr/bin/diff or its equivalent for Windows (GNU tools) is capable of ignoring certain differences corresponding to a regular expression.

Finally, running this roundtrip once more should produce two absolutely identical files.

-W


On 17 March 2015 at 14:27, Mirko Klemm <[hidden email]> wrote:
Hi Francesca,
The infoset of both XML files is actually identical. Is there a specific reason why you would like to set the namespace prefixes to a specific value? Standard-conforming XML software should not depend on namespace prefixes in a document.
Regards,
Mirko

> Am 2015-03-17 um 14:19 schrieb Herpertz, Francesca <[hidden email]>:
>
> 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&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:module name="B_stat" f001Filter="qualifier&gt;=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.


Reply | Threaded
Open this post in threaded view
|

Re: Unmarshall and Marshalling does not produce the same XML

Herpertz, Francesca
In reply to this post by Herpertz, Francesca
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 <[hidden email]>
Date: Dienstag, 17. März 2015 14:19
To: "[hidden email]" <[hidden email]>
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.
Reply | Threaded
Open this post in threaded view
|

Re: Unmarshall and Marshalling does not produce the same XML

Herpertz, Francesca
Hi, 

The Prefix Mapper would be the preferable option. Thank you for your help. 

I would compare with a diff not with a string compare as the order could be random. 
I am using java 7 and I can only find references to 1.6 instructions concerning the usage of the prefix mapper. Is there still a way to access the NamespacePrefixMapper in the newer Java version?

Kind regards, 
Francesca

From: <Herpertz>, Francesca Herpertz <[hidden email]>
Date: Dienstag, 17. März 2015 16:35
To: "[hidden email]" <[hidden email]>
Subject: Re: Unmarshall and Marshalling does not produce the same XML

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 <[hidden email]>
Date: Dienstag, 17. März 2015 14:19
To: "[hidden email]" <[hidden email]>
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.
Reply | Threaded
Open this post in threaded view
|

Re: Unmarshall and Marshalling does not produce the same XML

Aleksei Valikov
In reply to this post by Herpertz, Francesca
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 <[hidden email]> 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 <[hidden email]>
Date: Dienstag, 17. März 2015 14:19
To: "[hidden email]" <[hidden email]>
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.

Reply | Threaded
Open this post in threaded view
|

Re: Unmarshall and Marshalling does not produce the same XML

Wolfgang Laun-2
In reply to this post by Herpertz, Francesca
Does your XML Schema file define a targetNamespace? If you remove this and xjc-compile, the resulting Java code should not produce a namespace prefix.
-W

On 17 March 2015 at 17:07, Herpertz, Francesca <[hidden email]> wrote:
Hi, 

The Prefix Mapper would be the preferable option. Thank you for your help. 

I would compare with a diff not with a string compare as the order could be random. 
I am using java 7 and I can only find references to 1.6 instructions concerning the usage of the prefix mapper. Is there still a way to access the NamespacePrefixMapper in the newer Java version?

Kind regards, 
Francesca

From: <Herpertz>, Francesca Herpertz <[hidden email]>
Date: Dienstag, 17. März 2015 16:35
To: "[hidden email]" <[hidden email]>
Subject: Re: Unmarshall and Marshalling does not produce the same XML

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 <[hidden email]>
Date: Dienstag, 17. März 2015 14:19
To: "[hidden email]" <[hidden email]>
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.

Reply | Threaded
Open this post in threaded view
|

Re: Unmarshall and Marshalling does not produce the same XML

Iaroslav Savytskyi
Hi,

Also please consider about using latest standalone version of JAXB. It’s better to use standalone version over JDK ones.


Yarda

> On 17 Mar 2015, at 17:27, Wolfgang Laun <[hidden email]> wrote:
>
> Does your XML Schema file define a targetNamespace? If you remove this and xjc-compile, the resulting Java code should not produce a namespace prefix.
> -W
>
> On 17 March 2015 at 17:07, Herpertz, Francesca <[hidden email]> wrote:
> Hi,
>
> The Prefix Mapper would be the preferable option. Thank you for your help.
>
> I would compare with a diff not with a string compare as the order could be random.
> I am using java 7 and I can only find references to 1.6 instructions concerning the usage of the prefix mapper. Is there still a way to access the NamespacePrefixMapper in the newer Java version?
>
> Kind regards,
> Francesca
>
> From: <Herpertz>, Francesca Herpertz <[hidden email]>
> Date: Dienstag, 17. März 2015 16:35
> To: "[hidden email]" <[hidden email]>
> Subject: Re: Unmarshall and Marshalling does not produce the same XML
>
> 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 <[hidden email]>
> Date: Dienstag, 17. März 2015 14:19
> To: "[hidden email]" <[hidden email]>
> 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.
>

Reply | Threaded
Open this post in threaded view
|

Re: Unmarshall and Marshalling does not produce the same XML

Herpertz, Francesca
In reply to this post by Herpertz, Francesca
Hi, 

I have done some more research and came across the following article: 

I made some changes in my code according to this. Additionally I changed some things in my marshaling method. I left out the explicit setting of the namespace according to another stack overflow article: 

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/engine/xsd/validation.xsd");


OutputFormat format = new OutputFormat();

format.setIndent(true);

format.setNewlines(true);

XMLWriter writer = new XMLWriter(new FileOutputStream(new File(filepath)), format);

m.marshal(validation, writer);

writer.flush();

writer.close();

}


I tested it with the following option in my package-info.java: 

@XmlSchema(namespace = "http://somenamespace.com/ValidationType",

    xmlns = { 

        @XmlNs(prefix = "", namespaceURI = "http://somenamespace.com/ValidationType"),

    },

    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)


package com.somepackage.schema;


import javax.xml.bind.annotation.XmlNs;

import javax.xml.bind.annotation.XmlSchema;



But this will give me instead of ns2: as prefix ns3 like this:
<ns3:validation… 

When I change it to something like this: 

@XmlSchema(namespace = "http://somenamespace.com/ValidationType",

    xmlns = { 

        @XmlNs(prefix = "test", namespaceURI = "http://somenamespace.com/ValidationType"),

    },

    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)


package com.somepackage.schema;


import javax.xml.bind.annotation.XmlNs;

import javax.xml.bind.annotation.XmlSchema;


This will result in the expected behavior  like this: 
<test:validation… 

Is there something I am doing really really wrong? I would really appreciate some hints to solve this problem. 

Thank you for your help already. 

Kind regards, 
Francesca


From: <Herpertz>, Francesca Herpertz <[hidden email]>
Date: Dienstag, 17. März 2015 17:07
To: "[hidden email]" <[hidden email]>
Subject: Re: Unmarshall and Marshalling does not produce the same XML

Hi, 

The Prefix Mapper would be the preferable option. Thank you for your help. 

I would compare with a diff not with a string compare as the order could be random. 
I am using java 7 and I can only find references to 1.6 instructions concerning the usage of the prefix mapper. Is there still a way to access the NamespacePrefixMapper in the newer Java version?

Kind regards, 
Francesca

From: <Herpertz>, Francesca Herpertz <[hidden email]>
Date: Dienstag, 17. März 2015 16:35
To: "[hidden email]" <[hidden email]>
Subject: Re: Unmarshall and Marshalling does not produce the same XML

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 <[hidden email]>
Date: Dienstag, 17. März 2015 14:19
To: "[hidden email]" <[hidden email]>
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.
Reply | Threaded
Open this post in threaded view
|

Re: Unmarshall and Marshalling does not produce the same XML

Wolfgang Laun-2
Why don't you post the (reduced) XML Schema - I still think that removing targetNamespace is the simple solution you need.
-W

On 18 March 2015 at 13:41, Herpertz, Francesca <[hidden email]> wrote:
Hi, 

I have done some more research and came across the following article: 

I made some changes in my code according to this. Additionally I changed some things in my marshaling method. I left out the explicit setting of the namespace according to another stack overflow article: 

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/engine/xsd/validation.xsd");


OutputFormat format = new OutputFormat();

format.setIndent(true);

format.setNewlines(true);

XMLWriter writer = new XMLWriter(new FileOutputStream(new File(filepath)), format);

m.marshal(validation, writer);

writer.flush();

writer.close();

}


I tested it with the following option in my package-info.java: 

@XmlSchema(namespace = "http://somenamespace.com/ValidationType",

    xmlns = { 

        @XmlNs(prefix = "", namespaceURI = "http://somenamespace.com/ValidationType"),

    },

    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)


package com.somepackage.schema;


import javax.xml.bind.annotation.XmlNs;

import javax.xml.bind.annotation.XmlSchema;



But this will give me instead of ns2: as prefix ns3 like this:
<ns3:validation… 

When I change it to something like this: 

@XmlSchema(namespace = "http://somenamespace.com/ValidationType",

    xmlns = { 

        @XmlNs(prefix = "test", namespaceURI = "http://somenamespace.com/ValidationType"),

    },

    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)


package com.somepackage.schema;


import javax.xml.bind.annotation.XmlNs;

import javax.xml.bind.annotation.XmlSchema;


This will result in the expected behavior  like this: 
<test:validation… 

Is there something I am doing really really wrong? I would really appreciate some hints to solve this problem. 

Thank you for your help already. 

Kind regards, 
Francesca


From: <Herpertz>, Francesca Herpertz <[hidden email]>
Date: Dienstag, 17. März 2015 17:07
To: "[hidden email]" <[hidden email]>
Subject: Re: Unmarshall and Marshalling does not produce the same XML

Hi, 

The Prefix Mapper would be the preferable option. Thank you for your help. 

I would compare with a diff not with a string compare as the order could be random. 
I am using java 7 and I can only find references to 1.6 instructions concerning the usage of the prefix mapper. Is there still a way to access the NamespacePrefixMapper in the newer Java version?

Kind regards, 
Francesca

From: <Herpertz>, Francesca Herpertz <[hidden email]>
Date: Dienstag, 17. März 2015 16:35
To: "[hidden email]" <[hidden email]>
Subject: Re: Unmarshall and Marshalling does not produce the same XML

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 <[hidden email]>
Date: Dienstag, 17. März 2015 14:19
To: "[hidden email]" <[hidden email]>
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.

Reply | Threaded
Open this post in threaded view
|

Re: Unmarshall and Marshalling does not produce the same XML

Yoann Moranville
Hi,

I have no idea if that would help you but we had the "same" problem, we just wanted an output XML without the extra "ns2" namespace. It did not matter to us if the arguments were in a special order, but we just did not want to be bothered with an extra "ns2:" at every element when reading it (human reading).

For that we use JAXB 2.2.6 (probably other versions would allow it too - we use: com.sun.xml.bind / jaxb-impl with maven), and added a new property to the Marshaller:
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new OwnNamespaceMapper());

And this OwnNamespaceMapper class is extending com.sun.xml.bind.marshaller.NamespacePrefixMarshaller like that:
- getPreDeclaredNamespaceUris() would return a string array of all possible namespaces
- getPreferredPrefix(String namespaceUri, String default, boolean isRequired) would return the namespace prefix (empty string for example) if the namespaceUri is equal to your namespace, else the default (most likely where the ns2, ns3, ... comes from).

I am not sure this is what you are looking for (or if it written correctly), but for us it works fine and we have our output XML without any extra namespace prefixes, just a simple xmlns="..." at the root. Looks like what you were looking for.

Cheers,
Yoann


On 18/03/15 13:58, Wolfgang Laun wrote:
Why don't you post the (reduced) XML Schema - I still think that removing targetNamespace is the simple solution you need.
-W

On 18 March 2015 at 13:41, Herpertz, Francesca <[hidden email]> wrote:
Hi, 

I have done some more research and came across the following article: 

I made some changes in my code according to this. Additionally I changed some things in my marshaling method. I left out the explicit setting of the namespace according to another stack overflow article: 

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/engine/xsd/validation.xsd");


OutputFormat format = new OutputFormat();

format.setIndent(true);

format.setNewlines(true);

XMLWriter writer = new XMLWriter(new FileOutputStream(new File(filepath)), format);

m.marshal(validation, writer);

writer.flush();

writer.close();

}


I tested it with the following option in my package-info.java: 

@XmlSchema(namespace = "http://somenamespace.com/ValidationType",

    xmlns = { 

        @XmlNs(prefix = "", namespaceURI = "http://somenamespace.com/ValidationType"),

    },

    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)


package com.somepackage.schema;


import javax.xml.bind.annotation.XmlNs;

import javax.xml.bind.annotation.XmlSchema;



But this will give me instead of ns2: as prefix ns3 like this:
<ns3:validation… 

When I change it to something like this: 

@XmlSchema(namespace = "http://somenamespace.com/ValidationType",

    xmlns = { 

        @XmlNs(prefix = "test", namespaceURI = "http://somenamespace.com/ValidationType"),

    },

    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)


package com.somepackage.schema;


import javax.xml.bind.annotation.XmlNs;

import javax.xml.bind.annotation.XmlSchema;


This will result in the expected behavior  like this: 
<test:validation… 

Is there something I am doing really really wrong? I would really appreciate some hints to solve this problem. 

Thank you for your help already. 

Kind regards, 
Francesca


From: <Herpertz>, Francesca Herpertz <[hidden email]>
Date: Dienstag, 17. März 2015 17:07
To: "[hidden email]" <[hidden email]>
Subject: Re: Unmarshall and Marshalling does not produce the same XML

Hi, 

The Prefix Mapper would be the preferable option. Thank you for your help. 

I would compare with a diff not with a string compare as the order could be random. 
I am using java 7 and I can only find references to 1.6 instructions concerning the usage of the prefix mapper. Is there still a way to access the NamespacePrefixMapper in the newer Java version?

Kind regards, 
Francesca

From: <Herpertz>, Francesca Herpertz <[hidden email]>
Date: Dienstag, 17. März 2015 16:35
To: "[hidden email]" <[hidden email]>
Subject: Re: Unmarshall and Marshalling does not produce the same XML

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 <[hidden email]>
Date: Dienstag, 17. März 2015 14:19
To: "[hidden email]" <[hidden email]>
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.


Reply | Threaded
Open this post in threaded view
|

Re: Unmarshall and Marshalling does not produce the same XML

Herpertz, Francesca
In reply to this post by Herpertz, Francesca
Hi, 

Thanks for all your help. The issue had ist origin in the XSD as one of you guys said. 

The following construct will always result in an additional ns2: namespace:
 

<xs:complexType>

<xs:choice minOccurs="0" maxOccurs="unbounded">

<xs:annotation>

<xs:appinfo>

<jaxb:property name="types" />

</xs:appinfo>

</xs:annotation>

<xs:element name="a" type="vt:complexA" />

<xs:element name="b" type="vt:complexA" />

<xs:element name="c" type="vt:complexB" />

</xs:choice>

</xs:complexType>


Even if the unmarshal can handle this construct perfectly fine this will lead to the issue with the namespace in the marshaling . As soon as the "vt:complexA" complex type was copied, renamed and associated with the element "b" it worked with the standard mechanisms from JAXB and no ns2: namespace prefix was generated. The only real problem is now that I have to maintain both complex types in the XSD but this is a minor problem :). 


When changing the XSD to this: 


<xs:complexType>

<xs:choice minOccurs="0" maxOccurs="unbounded">

<xs:annotation>

<xs:appinfo>

<jaxb:property name="types" />

</xs:appinfo>

</xs:annotation>

<xs:element name="a" type="vt:complexA" />

<xs:element name="b" type="vt:complexB" />

<xs:element name="c" type="vt:complexC" />

</xs:choice>

</xs:complexType>


it  just works out of the box. 


Thanks people for your help. =) 


Kind regards, 

Francesca


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.