2013-03-01 100 views
1

我有一個Customer類和一個相關的xml文件。以字符串的形式將XMl傳遞給Jaxb Unmarshaller

  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方法?

回答

2

我可以提供XML文件作爲字符串

是的一部分,你只需要包裝在一個StringReader

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

非常感謝,這很有用。 – Pawan 2013-03-01 12:29:00