XML解析之DOM解析
程序员文章站
2022-05-29 08:19:47
...
为 XML 文档的已解析版本定义了一组接口。解析器读入整个文档,然后构建一个驻留内存的树结构,然后代码就可以使用 DOM 接口来操作这个树结构。优点:整个文档树在内存中,便于操作;支持删除、修改、重新排列等多种功能;缺点:将整个文档调入内存(包括无用的节点),浪费时间和空间;使用场合:一旦解析了文档还需多次访问这些数据;硬件资源充足(内存、CPU)。
package xml;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
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;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XMPParser{
private Document document ;
private static String fileName = "C:/Users/Administrator/Desktop/TestXml.xml";
//执行方法
public static void main(String[] args){
XMPParser parser = new XMPParser();
parser.init();
//创建XML文件
//parser.createXML(fileName);
//解析XML文件
parser.parserXML(fileName);
}
//初始化方法
public void init()
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
this.document = db.newDocument();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
//创建XML文件
public void createXML(String fileName)
{
//创建根节点employees
Element root = this.document.createElement("employees");
this.document.appendChild(root);
//创建employee子节点
Element employe = this.document.createElement("employee");
//创建employee子节点中的name节点
Element name = this.document.createElement("name");
//创建name子节点的文本值
name.setAttribute("style", "green");
name.appendChild(this.document.createTextNode("刘兴超"));
//把name子节点封装到employee节点中
employe.appendChild(name);
/**
* 创建sex节点
*/
Element sex = this.document.createElement("sex");
sex.appendChild(this.document.createTextNode("girl"));
employe.appendChild(sex);
/**
* 创建age节点
*/
Element age = this.document.createElement("age");
age.appendChild(this.document.createTextNode("30"));
employe.appendChild(age);
//把employe子节点封装到root根节点下
root.appendChild(employe);
TransformerFactory tf = TransformerFactory.newInstance();
try {
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
PrintWriter pw;
try {
pw = new PrintWriter(new FileOutputStream(fileName));
StreamResult result = new StreamResult(pw);
transformer.transform(source, result);
System.out.println("生成XML文件成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//解析XML文件
public void parserXML(String fileName) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(fileName);
NodeList employes = document.getChildNodes();
for(int i = 0 ; i< employes.getLength();i++)
{
Node node = employes.item(i);
System.out.println("node 1 =" + node.getNodeName());
NodeList employeeMeta = node.getChildNodes();
for (int k = 0; k < employeeMeta.getLength(); k++)
{
Node noded = employeeMeta.item(k);
System.out.println("node 2 =" + noded.getNodeName());
String nodeName = noded.getNodeName();
String TextContent = noded.getTextContent();
System.out.print(nodeName + ":" + TextContent);
System.out.println();
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
上一篇: [XML]Dom创建,解析xml
下一篇: xml数据解析--DOM解析