开发者

Add StartElement to JAXBContext marshal

I have an object x which I want to marshal into a file.

xml of x :

  <x id="0"><f1>bla</f1><f2>bla </f2><f3>b开发者_StackOverflow中文版la</f3></x>

this part I figured out ... but if I have a list of x's which I want to write to a file.

but I want to start the file with

<z>
   <y>
     <k>
       <x id="1"> ... </x>
       <x id="2"> ... </x>
       <x id="3"> ... </x>
     </k>
   </y>
</z>

is there anyway we can do this with marshel ?! add the z y and k then the list of x's and the close z y and k ?!

Thank you ...


You could leverage JAXB and StAX and do something like the following:

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

Output

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

FORMATTING

To get some formatting you could do:

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

}

Output

<?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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜