我有一个Java类,注释为XMLType
@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)
知道该怎么做?
谢谢!
答案 0 :(得分:1)
类似的东西:
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) {
...
}