標籤:creat attribute logs create 產生 system puts first doc
在查看公司架構的源碼的時候,發現架構用的是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"); }}
二、控制台列印結果如下:
根項目:personsname: Linux, content: 女, address: 湖北, phone: 155124578**name: Huhx, content: 男, address: 湖南, phone: 155124578**
友情連結
java基礎---->java內建的xml解析