users@jaxb.java.net

Re: Unmarshall and Marshalling does not produce the same XML

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Tue, 17 Mar 2015 17:27:00 +0100

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 <
francesca.herpertz_at_bearingpoint.com> 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 <
> francesca.herpertz_at_bearingpoint.com>
> Date: Dienstag, 17. März 2015 16:35
> To: "users_at_jaxb.java.net" <users_at_jaxb.java.net>
> 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 <
> 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.
>