我正在使用JAXB将Java对象编组为XML。

我想在其中放置此代码并返回xml的地方提供Web服务,但是我做马歇尔的最后一行不返回字符串,因为它是处理程序。

    JAXBContext context = JAXBContext.newInstance(Person.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Person person = new Person("Anonymous", 32, "employee");

    marshaller.marshal(person, System.out);


有人做过吗?

最佳答案

marshaller.marshal()有几个覆盖的方法,使您可以将对象编组到输出流,File,w3c.dom.Node ...等。

更好的问题可能是您正在使用什么编写Web服务。如果您使用的是JAX-WS,这些事情将自动为您完成。

http://jaxb.java.net/nonav/2.2.3/docs/api/javax/xml/bind/Marshaller.html

关于java - 使用JAXB将Java转换为XML,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6051428/

10-09 00:03