手艺人赵鹏

技术也是一门手艺

xml和字符串的转换

// 字符串转XMLString xmlStr = "";StringReader sr = new StringReader(xmlStr);InputSource is = new InputSource(sr);DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder=factory.newDocumentBuilder();Document doc = builder.parse(is);//XML转字符串TransformerFactory tf = TransformerFactory.newInstance();Transformer t = tf.newTransformer();t.setOutputProperty("encoding","utf-8");ByteArrayOutputStream bos = new ByteArrayOutputStream();t.transform(new DOMSource(doc), new StreamResult(bos));String xmlStr = bos.toString();

Leave a Reply