将XML文件加载到String变量中

时间:2019-01-10 19:54:37

标签: java

我想创建存储在String变量中的XML。我尝试过:

JAXB对象:

@XmlRootElement(name="sale")
public class Sale {

    String ssl_merchant_id;
    String ssl_user_id;
...
}


        Sale obj = new Sale();
        obj.setSsl_merchant_id("1233333");
        obj.setSsl_user_id("svsvsdv");

        try {

            File file = new File("C:\\file.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Sale.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(obj, file);
            jaxbMarshaller.marshal(obj, System.out);
            String value = jaxbMarshaller.marshal(obj, null);

        } catch (JAXBException e) {
            e.printStackTrace();
        }

如何将生成的XML存储到String变量中?

0 个答案:

没有答案