是的,是的,我知道关于这个话题问了很多问题。但是我仍然找不到解决我的问题的方法。我有一个带属性注释的Java对象。例如Customer,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);
我该怎么做呢?
https://stackoverflow.com/questions/26959343
复制相似问题