【问题标题】:Passing XMl to the Jaxb Unmarshaller in form of String以字符串的形式将 XMl 传递给 Jaxb Unmarshaller
【发布时间】:2013-02-15 21:31:54
【问题描述】:

我有一个 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 方法吗??

【问题讨论】:

    标签: jaxb


    【解决方案1】:

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

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

    String xml = "<foo><bar>Hello World</bar></foo>";
    StringReader reader = new StringReader(xml);
    Foo foo = (Foo) unmarshaller.unnmarshal(reader);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多