我想在字符串Type容器中获取生成的xml输出,并希望在控制台中显示该字符串。
try {
file = new File(XMLName);
JAXBContext jaxbContext = JAXBContext
.newInstance(ActivityXmlV1.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(activityXmlV1, file);
//jaxbMarshaller.marshal(activityXmlV1, System.out);
xmlData=asString(jaxbContext, activityXmlV1);
System.out.println(xmlData);
System.out.println("Sucess!!");
} catch (UnmarshalException ue) {
} catch (Exception e) {
}
答案 0 :(得分:0)
你可以这样做......
public String asString(JAXBContext pContext, Object pObject)throws JAXBException {
java.io.StringWriter sw = new StringWriter();
Marshaller marshaller = pContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.marshal(pObject, sw);
return sw.toString();
}
并调用此方法,您希望执行:
System.out.println(asString(jaxbContext,activityXmlV1));