将StartElement添加到JAXBContext marshal

时间:2011-07-14 18:20:31

标签: java xml jaxb marshalling xml-formatting

我有一个对象x,我想把它整理成一个文件。

xml of x:

  <x id="0"><f1>bla</f1><f2>bla </f2><f3>bla</f3></x>

这一部分我想通了......但是如果我有一个我要写入文件的x列表。

但我想用

启动文件
<z>
   <y>
     <k>
       <x id="1"> ... </x>
       <x id="2"> ... </x>
       <x id="3"> ... </x>
     </k>
   </y>
</z>

无论如何我们可以用马歇尔做到这一点吗? 添加z y和k然后是x的列表和关闭z y和k?

谢谢...

1 个答案:

答案 0 :(得分:0)

您可以利用JAXB和StAX并执行以下操作:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(X.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

        XMLOutputFactory xof = XMLOutputFactory.newFactory();
        XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);

        xsw.writeStartDocument();
        xsw.writeStartElement("x");
        xsw.writeStartElement("y");
        xsw.writeStartElement("k");

        X x1 = new X();
        x1.setId(1);
        JAXBElement<X> jaxbElement1 = new JAXBElement<X>(new QName("x1"), X.class, x1);
        marshaller.marshal(jaxbElement1, xsw);

        X x2 = new X();
        x2.setId(1);
        JAXBElement<X> jaxbElement2 = new JAXBElement<X>(new QName("x2"), X.class, x2);
        marshaller.marshal(jaxbElement2, xsw);

        X x3 = new X();
        x3.setId(3);
        JAXBElement<X> jaxbElement3 = new JAXBElement<X>(new QName("x3"), X.class, x3);
        marshaller.marshal(jaxbElement3, xsw);

        xsw.writeEndElement();
        xsw.writeEndElement();
        xsw.writeEndElement();
        xsw.writeEndDocument();
    }
}

<强>输出

<?xml version='1.0' encoding='UTF-8'?><x><y><k><x1 id="1" /><x2 id="1" /><x3 id="3" /></k></y></x>

<强>格式化

要获得一些格式化,您可以这样做:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(X.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

        XMLOutputFactory xof = XMLOutputFactory.newFactory();
        XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);

        xsw.writeStartDocument();
        xsw.writeCharacters("\n");
        xsw.writeStartElement("x");
        xsw.writeCharacters("\n\t");
        xsw.writeStartElement("y");
        xsw.writeCharacters("\n\t\t");
        xsw.writeStartElement("k");

        X x1 = new X();
        x1.setId(1);
        x1.setF1("x1f1");
        JAXBElement<X> jaxbElement1 = new JAXBElement<X>(new QName("x1"), X.class, x1);
        xsw.writeCharacters("\n\t\t\t");
        marshaller.marshal(jaxbElement1, xsw);

        X x2 = new X();
        x2.setId(1);
        x2.setF1("x2f1");
        JAXBElement<X> jaxbElement2 = new JAXBElement<X>(new QName("x2"), X.class, x2);
        xsw.writeCharacters("\n\t\t\t");
        marshaller.marshal(jaxbElement2, xsw);

        X x3 = new X();
        x3.setId(3);
        x3.setF1("x3f1");
        JAXBElement<X> jaxbElement3 = new JAXBElement<X>(new QName("x3"), X.class, x3);
        xsw.writeCharacters("\n\t\t\t");
        marshaller.marshal(jaxbElement3, xsw);

        xsw.writeCharacters("\n\t\t");
        xsw.writeEndElement();
        xsw.writeCharacters("\n\t");
        xsw.writeEndElement();
        xsw.writeCharacters("\n");
        xsw.writeEndElement();
        xsw.writeEndDocument();
    }

}

<强>输出

<?xml version='1.0' encoding='UTF-8'?>
<x>
    <y>
        <k>
            <x1 id="1"><f1>x1f1</f1></x1>
            <x2 id="1"><f1>x2f1</f1></x2>
            <x3 id="3"><f1>x3f1</f1></x3>
        </k>
    </y>
</x>