Jaxb generated xml - problem with root element pre

2019-02-04 23:17发布

I am trying to generate xml using jaxb. I created xsd and generated java classes. But when I generate xml, I am geeting prefix ns2 to the root tag, which I don't want.

ex: I want root tag to be

 <report>
   <id>rep 1</id>
</report>

, But getting as

<ns2:report>
....
</ns2:report>

In the generated java class, I gave annotation as @XmlRootElement(name="report",namespace="urn:report")

Can some one pls help

标签: java xml jaxb
5条回答
淡お忘
2楼-- · 2019-02-04 23:35

Take a look at this answer. It describes how to use a SAX Filter to remove any namespace.

查看更多
我命由我不由天
3楼-- · 2019-02-04 23:35

The blog entry Customizing JAXB shows the alternatives provided by implementing a PreferredMapper . Unfortunately it explains, that is not possible to fully suppress namespaces.

查看更多
何必那么认真
4楼-- · 2019-02-04 23:44

If this is your class:

package example;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="report",namespace="urn:report")
public class Root {

    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

}

Then it makes sense that there is a prefix on the root element, because you have specified that the "root" element is namespace qualified and the "id" element is not.

<ns2:report xmlns:ns2="urn:report">
    <id>123</id>
</ns2:report>

If you add a package-info class to your model, you can leverate the @XmlSchema annotation:

@XmlSchema(
        namespace = "urn:report",
        elementFormDefault = XmlNsForm.QUALIFIED)
package example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

Then the JAXB implementation may choose to leverage the default namespace, but note now all of the elements are namespace qualified which may or may not match your XML schema:

<report xmlns="urn:report">
    <id>123</id>
</report>

For more information on JAXB and namespaces see:

查看更多
放荡不羁爱自由
5楼-- · 2019-02-04 23:57

Use this attribute in your root element of your schema: elementFormDefault="qualified" So for instance:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
查看更多
狗以群分
6楼-- · 2019-02-04 23:57

Somehow the accepted answer did not work for me. I got success when I found solutions in some related stockOverflow questions involving DelegatingXMLStreamWriter from cxf and a filter, NoNamesWriter. The implementation I used with NoNamesWriter:

public class NoNamesWriter extends DelegatingXMLStreamWriter 
{
    @Override
    public void writeStartElement(String prefix, String local, String uri) throws XMLStreamException {
        delegate.writeStartElement("", local, uri);
    }   

    public static XMLStreamWriter filter(FileOutputStream fileOutputStream) throws XMLStreamException {
        return new NoNamesWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream));
    }

    public NoNamesWriter(XMLStreamWriter writer) {
        super(writer);
    }

}

Invoke the same as described here, like:

xmlmarshaller.marshal(xc, NoNamesWriter.filter(new FileOutputStream(outfile, false));

查看更多
登录 后发表回答