1.Document doc =
Jsoup.parse(pnrdatacontent,””,new Parser(new
XmlTreeBuilder()));
在做一般的XML数据交换过程中,我更乐意传递XML字符串,而不是格式化的XML
Document。这就涉及到XML字符串和Xml
Document的转换问题,说白了这是个很简单的问题,本文就各种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);
//XML转字符串
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.setOutputProperty(“encoding”,”GB23121″);//解决中文问题,试过用