JAXB Fragment Marshal没有命名空间

时间:2012-02-15 17:08:48

标签: java xml jaxb marshalling

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

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

以下是我使用上述创建的代码:

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();

3 个答案:

答案 0 :(得分:9)

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

<强>演示

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

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();
    }

}

<强>工作集

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

package forum9297872;

import javax.xml.bind.annotation.XmlRootElement;

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

}

<强>输出

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

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

答案 1 :(得分:1)

有三种解决方法。

1)为您的workersets容器创建JAXB注释对象。将workersets添加到该对象,然后整理整个事件。

2)按照101 ways to marshal objects with JAXB中的第一个示例,使用DocumentBuilderFactory识别名称空间。

3)假设jaxb对象位于一个永远不具有限定名称空间的包中,您可以将以下内容添加到包注释中:(注意:已经有一段时间了,因为我已经完成了这个并且我没有测试过这个代码)

@XmlSchema(namespace = "", elementFormDefault = XmlNsForm.UNQUALIFIED) 
package example;

答案 2 :(得分:0)

另一个例子:

    try {
        JAXBContext customerInformationRequestContext = JAXBContext.newInstance(CustomerInformationRequest.class);
        Unmarshaller unmarshaller = customerInformationRequestContext.createUnmarshaller();
        StringReader stringReader = new StringReader(requestPayload);
        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(stringReader);
        XMLStreamReaderWrapper reader = new XMLStreamReaderWrapper(xsr);
        CustomerInformationRequest customerInformationRequest = (CustomerInformationRequest) unmarshaller.unmarshal(reader);
        CustomerInformationResponse customerInformationResponse = customerLookup(customerInformationRequest, sessionTransaction);
        JAXBContext customerInformationResponseContext = JAXBContext.newInstance(CustomerInformationResponse.class);
        Marshaller marshaller = customerInformationResponseContext.createMarshaller();
        StringWriter stringWriter = new StringWriter();
        XMLOutputFactory xof = XMLOutputFactory.newFactory();
        XMLStreamWriter xsw = xof.createXMLStreamWriter(stringWriter);
        xsw = new XMLStreamWriterWrapper(xsw);
        marshaller.marshal(customerInformationResponse, xsw);
        String responsePayload = stringWriter.toString();
        return responsePayload;
    } catch (Exception e) {
        log.debug("The payload could not be unmarshalled.", e);
        return null;
    }