将 StartElement 添加到 JAXBContext marshal

发布于 2024-11-20 00:00:26 字数 550 浏览 5 评论 0原文

我有一个对象 x,我想将其编组到文件中。

x 的 xml :

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

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

但我想用 marshel 来启动文件,

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

我们是否可以用 marshel 来做到这一点?! 添加 zy 和 k,然后添加 x 列表以及接近的 zy 和 k ?!

谢谢 ...

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>bla</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 ...

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

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

发布评论

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

评论(1

本宫微胖 2024-11-27 00:00:26

您可以利用 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>

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