在查看公司框架的源码的时候,发现框架用的是jdk自带的xml解析。今天,想着对它总结一下。从未放弃过爱你,只是从浓烈变得悄无声气。
利用jdk自带的xml创建文档
一、 CreateXmlFile类如下:
package com.huhx.saxxml; import java.io.FileOutputStream; import java.io.PrintWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; public class CreateXmlFile { public static void parseXml(String path) { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder dombuilder = builderFactory.newDocumentBuilder(); Document document = dombuilder.newDocument(); Element root = document.createElement("persons"); document.appendChild(root); // 第一个人 Element person1 = document.createElement("person"); person1.setAttribute("name", "Linux"); root.appendChild(person1); Element sex = document.createElement("sex"); sex.appendChild(document.createTextNode("女")); person1.appendChild(sex); Element address = document.createElement("address"); address.appendChild(document.createTextNode("湖北")); person1.appendChild(address); Element phoneNumber = document.createElement("phone"); phoneNumber.appendChild(document.createTextNode("155124578**")); person1.appendChild(phoneNumber); // 第二个人 Element person2 = document.createElement("person"); person2.setAttribute("name", "Huhx"); root.appendChild(person2); Element sex2 = document.createElement("sex"); sex2.appendChild(document.createTextNode("男")); person2.appendChild(sex2); Element address2 = document.createElement("address"); address2.appendChild(document.createTextNode("湖南")); person2.appendChild(address2); Element phoneNumber2 = document.createElement("phone"); phoneNumber2.appendChild(document.createTextNode("155124578**")); person2.appendChild(phoneNumber2); // 将上述的document写入到文件中 TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); PrintWriter pw = new PrintWriter(new FileOutputStream(path)); StreamResult result = new StreamResult(pw); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { parseXml("text.xml"); } }
二、 生成的text.xml文件如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <persons> <person name="Linux"> <sex>女</sex> <address>湖北</address> <phone>155124578**</phone> </person> <person name="Huhx"> <sex>男</sex> <address>湖南</address> <phone>155124578**</phone> </person> </persons>
利用jdk自带的xml解析文档
一、 ReadXmlFile.java类的内容如下:
package com.huhx.saxxml; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class ReadXmlFile { public static void readXml(String path) { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); Document document = builder.parse(path); Element rootElement = document.getDocumentElement(); System.out.println("根元素:" + rootElement.getNodeName()); NodeList childNodes = rootElement.getElementsByTagName("person"); for (int i = 0; i < childNodes.getLength(); i++) { Element element = (Element) childNodes.item(i); String name = element.getAttribute("name"); String content = element.getElementsByTagName("sex").item(0).getFirstChild().getNodeValue(); String address = element.getElementsByTagName("address").item(0).getFirstChild().getNodeValue(); String phone = element.getElementsByTagName("phone").item(0).getFirstChild().getNodeValue(); System.out.println("name: " + name + ", content: " + content + ", address: " + address + ", phone: " + phone); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { readXml("text.xml"); } }
二、控制台打印结果如下:
根元素:persons name: Linux, content: 女, address: 湖北, phone: 155124578** name: Huhx, content: 男, address: 湖南, phone: 155124578**