【发布时间】:2015-01-13 13:50:57
【问题描述】:
是的,是的,我知道有人就这个话题提出了很多问题。但我仍然找不到解决问题的方法。我有一个属性注释的 Java 对象。例如客户,like in this example。我想要它的字符串表示。 Google 建议将 JAXB 用于此类目的。但在所有示例中,创建的 XML 文件都会打印到文件或控制台,如下所示:
File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(customer, file);
jaxbMarshaller.marshal(customer, System.out);
但我必须使用这个对象并以 XML 格式通过网络发送。所以我想得到一个代表 XML 的字符串。
String xmlString = ...
sendOverNetwork(xmlString);
我该怎么做?
【问题讨论】: