JAXB Fragment Marshal没有命名空间 [英] JAXB Fragment Marshal w/o namespace

查看:159
本文介绍了JAXB Fragment Marshal没有命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我的marshaller的JAXB_FRAGMENT属性来编组WorkSet级别。问题在于,当我封送它时,每次都为WorkSet元素提供xmlns属性。有没有办法编组,以便它不附加xmlns属性?这是我的XML的样子。

I'm using the JAXB_FRAGMENT property for my marshaller to marshal at the WorkSet level. The problem is that when I marshal it's giving the WorkSet element the xmlns attribute everytime. Is there a way to marshal so that it doesn't attach the xmlns attribute? Here's what my XML looks like.

    <Import>
        <WorkSets>
            <WorkSet xmlns="http://www.namespace.com">
                <Work>
                <Work>
                ...
                ..
                ...
            </WorkSet>
            <WorkSet xmlns="http://www.namespace.com">
                <Work>
                <Work>
                ...
            </WorkSet>
        </WorkSets>
    </Import>

以下是我正在使用上面创建的代码:

Here's the code I'm using the create the above:

FileOutputStream fos = new FileOutputStream("import.xml");
XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(fos);

JAXBContext jc = JAXBContext.newInstance(WorkSet.class);
Marshaller m = jc.createMarshaler();
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

writer.writeStartDocument();
writer.writeStartElement("Import");
writer.writeAttribute("xmlns","http://www.namespace.com");
writer.writeStartElement("WorkSets");

while(hasWorkSet){
m.marshal(workSet, writer)
}
writer.writeEndDocument();
writer.close();


推荐答案

假设您希望文档的默认命名空间为 http://www.namespace.com ,您可以执行以下操作:

Assuming you want the default namespace for your document to be http://www.namespace.com, you could do the following:

演示

XMLStreamWriter.setDefaultNamespace(String) XMLStreamWriter.writeNamespace(String,字符串)方法将用于设置和写入XML文档的默认命名空间。

The XMLStreamWriter.setDefaultNamespace(String) and XMLStreamWriter.writeNamespace(String, String) methods will be used to set and write the default namespace for the XML document.

package forum9297872;

import javax.xml.bind.*;
import javax.xml.stream.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out);
        writer.setDefaultNamespace("http://www.namespace.com");

        JAXBContext jc = JAXBContext.newInstance(WorkSet.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

        writer.writeStartDocument();
        writer.writeStartElement("http://www.namespace.com", "Import");
        writer.writeNamespace("", "http://www.namespace.com");
        writer.writeStartElement("WorkSets");

        m.marshal(new WorkSet(), writer);
        m.marshal(new WorkSet(), writer);

        writer.writeEndDocument();
        writer.close();
    }

}

WorkSet

我的假设是您在JAXB模型中指定了命名空间信息。

My assumption is that you have specified namespace information in your JAXB model.

package forum9297872;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="WorkSet", namespace="http://www.namespace.com")
public class WorkSet {

}

输出

以下是运行演示代码的输出:

Below is the output from running the demo code:

<?xml version="1.0" ?><Import xmlns="http://www.namespace.com"><WorkSets><WorkSet></WorkSet><WorkSet></WorkSet></WorkSets></Import>

这篇关于JAXB Fragment Marshal没有命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆