Java代码
1.// 字符串转XML
2.
3.Java代码
4.1.String xmlStr = \“......\”;
5.2.StringReader sr = new StringReader(xmlStr);
6.3.InputSource is = new InputSource(sr);
7.4.DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
8.5.DocumentBuilder builder=factory.newDocumentBuilder();
9.6.Document doc = builder.parse(is);
10.String xmlStr = \“......\”;
11.StringReader sr = new StringReader(xmlStr);
12.InputSource is = new InputSource(sr);
13.DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
14.DocumentBuilder builder=factory.newDocumentBuilder();
15.Document doc = builder.parse(is);
16.
17.
18.//XML转字符串
19.
20.Java代码
21.1.TransformerFactory tf = TransformerFactory.newInstance();
22.2.Transformer t = tf.newTransformer();
23.3.t.setOutputProperty(\“encoding\”,\“GB23121\”);//解决中文问题,试过用GBK不行
24.4.ByteArrayOutputStream bos = new ByteArrayOutputStream();
25.5.t.transform(new DOMSource(doc), new StreamResult(bos));
26.6.String xmlStr = bos.toString();
27.TransformerFactory tf = TransformerFactory.newInstance();
28.Transformer t = tf.newTransformer();
29.t.setOutputProperty(\“encoding\”,\“GB23121\”);//解决中文问题,试过用GBK不行
30.ByteArrayOutputStream bos = new ByteArrayOutputStream();
31.t.transform(new DOMSource(doc), new StreamResult(bos));
32.String xmlStr = bos.toString();
本文出自 “网络侠客” 博客,谢绝转载!