XML-DOM解析方式

·关键类
DocumentBuilderFactory
DocumentBuilder
TransformerFactory
Transformer
DOMSource
Result
w3c DOM相关类

 

·创建xml文件
-创建Document
 DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
 DocumentBuilder builder = fac.newDocumentBuilder();
 Document doc = builder.newDocument();
-添加DOM元素
 ....(略,见dom操作说明)
-创建xml源和结果树(xml输出)
 Source src = new DOMSource(doc);
 Result xmlResult = new StreamResult(out);
-创建转化器,输出
 TransformerFactory tf = TransformerFactory.newInstance();
 Transformer transformer = tf.newTransformer();
 transformer.transform(src, xmlResult);

·dom操作说明
 Document doc = builder.newDocument();
 Element root = doc.createElement("root");
 doc.appendChild(root);
 doc.createTextNode("");
 ....

 

·说明:
-Source可以接受各种源
 DOMSource
 SAXSource
 StAXSource
 StreamSource
-Result可以向各种输出流输出,类似于StreamResult类型的还有,
 DOMResult
 SAXResult
 StAXResult
 StreamResult

你可能感兴趣的:(xml)