gpt4 book ai didi

java - 将 StartElement 添加到 JAXBContext marshal

转载 作者:行者123 更新时间:2023-11-29 09:20:35 25 4
gpt4 key购买 nike

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

x 的 xml:

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

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

谢谢...

最佳答案

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

关于java - 将 StartElement 添加到 JAXBContext marshal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6698062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com