users@jaxb.java.net

Re: Unmarshall and Marshalling does not produce the same XML

From: Yoann Moranville <yoann.moranville_at_gmail.com>
Date: Wed, 18 Mar 2015 17:29:40 +0100

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
> <francesca.herpertz_at_bearingpoint.com
> <mailto:francesca.herpertz_at_bearingpoint.com>> wrote:
>
> Hi,
>
> I have done some more research and came across the following article:
> http://hwellmann.blogspot.de/2011/03/jaxb-marshalling-with-custom-namespace.html?m=1
>
> 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
> <francesca.herpertz_at_bearingpoint.com
> <mailto:francesca.herpertz_at_bearingpoint.com>>
> Date: Dienstag, 17. März 2015 17:07
> To: "users_at_jaxb.java.net <mailto:users_at_jaxb.java.net>"
> <users_at_jaxb.java.net <mailto:users_at_jaxb.java.net>>
> 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
> <francesca.herpertz_at_bearingpoint.com
> <mailto:francesca.herpertz_at_bearingpoint.com>>
> Date: Dienstag, 17. März 2015 16:35
> To: "users_at_jaxb.java.net <mailto:users_at_jaxb.java.net>"
> <users_at_jaxb.java.net <mailto: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
> <mailto:francesca.herpertz_at_bearingpoint.com>>
> Date: Dienstag, 17. März 2015 14:19
> To: "users_at_jaxb.java.net <mailto:users_at_jaxb.java.net>"
> <users_at_jaxb.java.net <mailto: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"?>
>
> <validationxmlns: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:validationxmlns: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(newFile("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.
>
>