首页 文章

JAXB Fragment Marshal没有命名空间

提问于
浏览
11

我正在使用我的marshaller的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 回答

  • 8

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

    Demo

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

    WorkSet

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

    package forum9297872;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement(name="WorkSet", namespace="http://www.namespace.com")
    public class WorkSet {
    
    }
    

    Output

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

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

    有三种解决方法 .

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

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

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

    @XmlSchema(namespace = "", elementFormDefault = XmlNsForm.UNQUALIFIED) 
    package example;
    
  • 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;
        }
    

相关问题