我有一个标注为XMLType的Java类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "fooClass", propOrder = {
"fooElement1",
"fooElement2"
})
public class fooClass{
@XmlElement(required = true)
protected String fooElement1;
@XmlElement(required = true)
protected String fooElement2;
.....
}
我希望能够提取Java中的XML表示形式(最好是流形式,但也可以使用String形式),大致如下:
fooClass foo = new fooClass()
foo.setFooElement1("baba")
foo.setFooElement2("abab")
String xmlRep = DomSomething(foo)
知道该怎么办吗?
谢谢!
就像是:
try {
JAXBContext jaxbContext = JAXBContext.newInstance(fooClass.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
jaxbMarshaller.marshal(fooInstance, ps);
String result = new String(baos.toByteArray());
...
} catch (JAXBException e) {
...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句