简体   繁体   中英

JAXB and marshalling

I have the following:

JAXBContext context = JAXBContext.newInstance(A.class, B.class, C.class, D.class);

Let's say A, B, C, D are classes from different schemas having different namespaces.

I am creating the Marshaller object as follows:

Marshaller m = context.createMarshaller();

Then I use this Marshaller m to convert an instance of A to XML.

When the XML code is generated, the Marshaller object also puts the other namespaces associated with B, C and D.

I see something like the following:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:A 
xmlns:ns4="http://a.com/" 
xmlns:ns5="http://b.com/"
xmlns:ns6="http://c.com/"
xmlns:ns7="http://d.com/">
<ns4:Foo></ns4:Foo>
</ns4:A>

I don't want ns5, ns6 and ns7 getting added to the XML. How do I do it?

Thanks in advance!

bind a namespace to the classes in a package, using package-info.java whilch will ook like this:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://nameSpaceUri"
, xmlns = {
    @XmlNs(prefix = "myPrefix", namespaceURI = "http://nameSpaceUri")
}
, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)

package my.package.;

if no namespace is applied to a marshalled object this

<ns4:A 
xmlns:ns4="http://a.com/" 
xmlns:ns5="http://b.com/"
xmlns:ns6="http://c.com/"
xmlns:ns7="http://d.com/">

will happen.

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

The behaviour you are seeing will differ upon which implemenation of JAXB you are using. The MOXy implementation will give you the result you are looking for. I will demonstrate below.

JAVA MODEL

Below is the Java model that will be used for this example. The package level @XmlSchema annotation will be used to specify the namespace qualfication in each of the packages (see: http://blog.bdoughan.com/2010/08/jaxb-namespaces.html ).

forum13408684.aA

package forum13408684.a;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class A {

}

forum13408684.a.package-info

@XmlSchema(namespace="A", elementFormDefault=XmlNsForm.QUALIFIED)
package forum13408684.a;

import javax.xml.bind.annotation.*;

forum13408684.bB

package forum13408684.a;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class B {

}

forum13408684.b.package-info

@XmlSchema(namespace="B", elementFormDefault=XmlNsForm.QUALIFIED)
package forum13408684.b;

import javax.xml.bind.annotation.*;

DEMO CODE

package forum13408684;

import javax.xml.bind.*;
import forum13408684.a.A;
import forum13408684.b.B;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(A.class, B.class);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(new A(), System.out);
        marshaller.marshal(new B(), System.out);
    }

}

OUTPUT

JAXB Reference Implementation

The output below matches what you described in your question:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a xmlns="A" xmlns:ns2="B"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:b xmlns="A" xmlns:ns2="B"/>

EclipseLink JAXB (MOXy)

If you specify MOXy as your JAXB provider (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html ) you will get the output you are looking for.

<?xml version="1.0" encoding="UTF-8"?>
<a xmlns="A"/>
<?xml version="1.0" encoding="UTF-8"?>
<ns0:b xmlns:ns0="B"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM