本人微信公众号"aeolian"~

xml解析

使用最原始的javax.xml.parsers,标准的jdk api

// 字符串转XML
String xmlStr = "......";
StringReader sr = new StringReader(xmlStr); 
InputSource is = new InputSource(sr); 
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder=factory.newDocumentBuilder(); 
Document doc = builder.parse(is);
NodeList list = root.getChildNodes();
//XML转字符串 TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.setOutputProperty("encoding","GB23121");//解决中文问题,试过用GBK不行 ByteArrayOutputStream bos = new ByteArrayOutputStream(); t.transform(new DOMSource(doc), new StreamResult(bos)); String xmlStr = bos.toString(); 这里的XML DOCUMENT为org.w3c.dom.Document

 

点赞

Leave a Reply

Your email address will not be published. Required fields are marked *