我正在使用 JAXB 将 Java 对象编码为 XML。
我想创建一个 Web 服务,在其中放置此代码并返回 xml,但我执行 marshall 的最后一行不会返回字符串,因为它是一个处理程序。
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()
有几个重写方法,允许您将对象编码到输出流、文件、w3c.dom.Node ...等中。
更好的问题可能是您使用什么来编写 Web 服务。如果您使用 JAX-WS,这些事情会自动为您完成。
http://jaxb.java.net/nonav/2.2.3/docs/api/javax/xml/bind/Marshaller.html
关于使用 JAXB 将 Java 转换为 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6051428/