Java 如何在运行时使用MOXy从列表中的对象封送特定字段

Java 如何在运行时使用MOXy从列表中的对象封送特定字段,java,jaxb,eclipselink,marshalling,moxy,Java,Jaxb,Eclipselink,Marshalling,Moxy,我需要将集合列表封送到xml,但我希望用户选择将从列表对象封送哪些字段。以下是我试图做的更好的解释 首先,我有一个名为服务器的POJO @XmlAccessorType(XmlAccessType.FIELD) public class Server { @XmlElement(nillable=true) private String vendor; @XmlElement(nillable=true) private int memory; @XmlElemen

我需要将集合列表封送到
xml
,但我希望用户选择将从列表对象封送哪些字段。以下是我试图做的更好的解释

首先,我有一个名为服务器的POJO

@XmlAccessorType(XmlAccessType.FIELD)
public class Server {

    @XmlElement(nillable=true)  private String vendor;
    @XmlElement(nillable=true)  private int memory;
    @XmlElement(nillable=true)  private String cpu;
//getters and setters
}
然后,在我的应用程序中,我使用了这个方法来执行封送处理工作。 请注意,此实现基于此

然后我想得到的xml是

<?xml version="1.0" encoding="UTF-8"?>
<servers>
    <server>
        <memory>4096</memory>
        <cpu>intel</cpu>
    </server>
    <server>
        <memory>2048</memory>
        <cpu>amd</cpu>
    </server>
</servers>

4096
英特尔
2048
amd
谁能帮我找出我做错了什么? 是否有其他方法可以仅封送列表中服务器对象中的选定字段


谢谢

我们在MOXy中使用
@xmlanyement(lax=true)
处理对象图时遇到了一个错误。您可以使用下面的bug来跟踪我们在这个问题上的进展

变通办法 对于这个用例,您可以利用JAXB和StAX。StAX将用于编写包装器元素,然后您可以将
Server
的各个实例封送到它。请注意,对象图的创建方式略有变化

    XMLOutputFactory xof = XMLOutputFactory.newFactory();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);
    xsw.writeStartDocument();
    xsw.writeStartElement("servers");

    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

    ObjectGraph outputInfo = JAXBHelper.getJAXBContext(jc)
        .createObjectGraph(Server.class);
    for (String o : output) {
        outputInfo.addAttributeNodes(o);
    }

    marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, outputInfo);

    for(Object item : ls) {
        marshaller.marshal(item, xsw);
    }
    xsw.writeEndElement();
    xsw.writeEndDocument();
    xsw.close();

我们在MOXy中使用
@xmlanyement(lax=true)
处理对象图时遇到了一个错误。您可以使用下面的bug来跟踪我们在这个问题上的进展

变通办法 对于这个用例,您可以利用JAXB和StAX。StAX将用于编写包装器元素,然后您可以将
Server
的各个实例封送到它。请注意,对象图的创建方式略有变化

    XMLOutputFactory xof = XMLOutputFactory.newFactory();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);
    xsw.writeStartDocument();
    xsw.writeStartElement("servers");

    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

    ObjectGraph outputInfo = JAXBHelper.getJAXBContext(jc)
        .createObjectGraph(Server.class);
    for (String o : output) {
        outputInfo.addAttributeNodes(o);
    }

    marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, outputInfo);

    for(Object item : ls) {
        marshaller.marshal(item, xsw);
    }
    xsw.writeEndElement();
    xsw.writeEndDocument();
    xsw.close();

尝试上面的断言(stringo:output),效果是assert output.size()>0:“输出中没有字符串!”@BillHorvathII我的代码中有断言。我只是没有把它写在我的帖子里。感谢上面为(stringo:output)的断言,其效果是assert-output.size()>0:“输出中没有字符串!”@BillHorvathII我的代码中有断言。我只是没有把它写在我的帖子里。谢谢
Server[vendor=HP,memory=4096,cpu=intel]
Server[vendor=IBM,memory=2048,cpu=amd]
<?xml version="1.0" encoding="UTF-8"?>
<servers>
    <server>
        <memory>4096</memory>
        <cpu>intel</cpu>
    </server>
    <server>
        <memory>2048</memory>
        <cpu>amd</cpu>
    </server>
</servers>
    XMLOutputFactory xof = XMLOutputFactory.newFactory();
    XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);
    xsw.writeStartDocument();
    xsw.writeStartElement("servers");

    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

    ObjectGraph outputInfo = JAXBHelper.getJAXBContext(jc)
        .createObjectGraph(Server.class);
    for (String o : output) {
        outputInfo.addAttributeNodes(o);
    }

    marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, outputInfo);

    for(Object item : ls) {
        marshaller.marshal(item, xsw);
    }
    xsw.writeEndElement();
    xsw.writeEndDocument();
    xsw.close();