JAXB Fragment Marshal 不带命名空间

发布于 2025-01-05 17:31:54 字数 1204 浏览 4 评论 0原文

我使用 JAXB_FRAGMENT 属性让编组器在工作集级别进行编组。问题是,当我编组时,它每次都会为 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();

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海里的那抹蓝 2025-01-12 17:31:54

假设您希望文档的默认命名空间为 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 {

}

输出

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

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

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

Demo

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

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 {

}

Output

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>
糖果控 2025-01-12 17:31:54

对此有三种解决方法。

1) 为工作集的容器创建 JAXB 带注释的对象。将工作集添加到该对象,然后整理整个对象。

2) 按照101 种使用 JAXB 封送对象的方法中的第一个示例< /a> 并使用具有命名空间感知的 DocumentBuilderFactory

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

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

There are three workarounds for this.

1) Create JAXB annotated objects for the container of your workersets. Add the workersets to that object and then marshal the whole thing.

2) Follow the first example in 101 ways to marshal objects with JAXB and use DocumentBuilderFactory with namespace aware.

3) Assuming that the jaxb object is in a package that should never have qualified namespaces you can add the following to the package annotation: (note: it's been a while since i've done this and I havn't tested this code)

@XmlSchema(namespace = "", elementFormDefault = XmlNsForm.UNQUALIFIED) 
package example;
热情消退 2025-01-12 17:31:54

再举个例子:

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

Just another example:

    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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文