1

我有一个 Customer 类和一个相关的 xml 文件。

            File file = new File("D:\\TestingData\\customer.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Customer data = (Customer) jaxbUnmarshaller.unmarshal(file);

我需要将 customer.xml 文件放在外部位置。(D:\TestingData\customer.xml)

我的问题是我可以将 xml 文件作为字符串的一部分提供给 unmarshall 方法吗?

4

1 回答 1

2

我可以将 xml 文件作为字符串的一部分提供吗

是的,您只需将其包装在StringReader.

String xml = "<foo><bar>Hello World</bar></foo>";
StringReader reader = new StringReader(xml);
Foo foo = (Foo) unmarshaller.unnmarshal(reader);
于 2013-03-01T11:37:49.680 回答