首页 > 代码库 > java基础---->java自带的xml解析
java基础---->java自带的xml解析
在查看公司框架的源码的时候,发现框架用的是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解析
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。