String转XML的方法
w3c的dom
Java代码
1.DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
2. DocumentBuilder builder;
3. try {
4. builder = factory.newDocumentBuilder();
5. Document doc = builder.parse( new ByteArrayInputStream(str.getBytes()));
6. } catch (ParserConfigurationException e) {
7. // TODO Auto-generated catch block
8. e.printStackTrace();
9. } catch (SAXException e) {
10. // TODO Auto-generated catch block
11. e.printStackTrace();
12. } catch (IOException e) {
13. // TODO Auto-generated catch block
14. e.printStackTrace();
15. }
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
Document doc = builder.parse( new ByteArrayInputStream(str.getBytes()));
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dom4j
Java代码
1.SAXReader saxReader = new SAXReader();
2. Document document;
3. try {
4. document = saxReader.read(new ByteArrayInputStream(str.getBytes()));
5. Element incomingForm = document.getRootElement();
6. } catch (DocumentException e) {
7. // TODO Auto-generated catch block
8. e.printStackTrace();
9. }