读xml
随便创建一个users.xml
<?xml version="1.0" encoding="UTF-8"?>
<users>
<user id="0">
<name>Mary</name>
<age>23</age>
<sex>Female</sex>
</user>
<user id="1">
<name>Mike</name>
<age>24</age>
<sex>Male</sex>
</user>
<user id="2">
<name>Alice</name>
<age>23</age>
<sex>Female</sex>
</user>
<user id="3">
<name>Tom</name>
<age>24</age>
<sex>Male</sex>
</user>
</users>
java代码
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DomReader {
public static void main(String[] args) {
// 从上而下进行访问
recursiveTraverse();
System.out.println("=====分割线======");
// 根据名称进行搜索
traverseBySearch();
}
private static void traverseBySearch() {
try {
// 采用Dom解析xml文件
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse("users.xml");
Element rootElement = document.getDocumentElement();
NodeList nodeList = rootElement.getElementsByTagName("name");
if (nodeList != null) {
for (int i = 0; i < nodeList.getLength(); i++) {
Element element = (Element) nodeList.item(i);
System.out.println(element.getNodeName() + " = " + element.getTextContent());
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void recursiveTraverse() {
try {
// 采用Dom解析xml文件
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse("users.xml");
// 获取所有的一级子节点
NodeList usersList = document.getChildNodes();
System.out.println(usersList.getLength());
for (int i = 0; i < usersList.getLength(); i++) {
Node users = usersList.item(i);
// 获取二级子节点user的列表
NodeList userList = users.getChildNodes();
System.out.println("==" + userList.getLength());
for (int j = 0; j < userList.getLength(); j++) {
Node user = userList.item(j);
if (user.getNodeType() == Node.ELEMENT_NODE) {
NodeList metaList = user.getChildNodes();
System.out.println("====" + metaList.getLength());
for (int k = 0; k < metaList.getLength(); k++) {
// 到最后 一级文本
Node meta = metaList.item(k);
if (meta.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(meta.getNodeName());
}
}
}
}
}
} catch (ParserConfigurationException | SAXException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出结果
1
==9
====7
name
age
sex
====7
name
age
sex
====7
name
age
sex
====7
name
age
sex
=====分割线======
name = Mary
name = Mike
name = Alice
name = Tom
写xml
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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 DomWriter {
public static void main(String[] args) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// 新创建一个Document节点
Document document = db.newDocument();
if (document != null) {
// 都是采用document创建元素
Element doc = document.createElement("document");
Element element = document.createElement("element");
// element增加2个属性
element.setAttribute("type", "paragraph");
element.setAttribute("alignment", "left");
Element object = document.createElement("object");
object.setAttribute("type", "text");
Element text = document.createElement("text");
// 给text节点赋值
text.appendChild(document.createTextNode("abcdefg"));
Element bold = document.createElement("bold");
// 给bold节点赋值
bold.appendChild(document.createTextNode("true"));
// 把text节点挂在object下
object.appendChild(text);
// 把bold节点挂在object下
object.appendChild(bold);
// 把object节点挂在element下
element.appendChild(object);
// 把element节点挂在doc下
doc.appendChild(element);
// 把doc节点挂在document下
document.appendChild(doc);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
//定义目标文件
File file = new File("dom_result.xml");
StreamResult result = new StreamResult(file);
//将xml内容写入到文件中
transformer.transform(source, result);
System.out.println("write xml file successfully");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出文件内容
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document>
<element alignment="left" type="paragraph">
<object type="text">
<text>abcdefg</text>
<bold>true</bold>
</object>
</element>
</document>