xml文档主要有两种解析方式,DOM接口或SAX接口解析。
Dom解析示例:
1.建立DocumentBuilderFactory
2.建立DocumentBuilder
3.建立Document
4.建立NodeList
5.进行xml信息读取
xml文件放在D盘,为dom_demo_02.xml
<?xml version="1.0" encoding="GBK"?>
<addresslist>
<linkman>
<name> 张三 </name>
<email>1522255555@qq.com</email>
</linkman>
<linkman>
<name> 李四 </name>
<email>15222534355@qq.com</email>
</linkman>
</addresslist>
解析代码为:
import java.io.File;
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.NodeList;
import org.xml.sax.SAXException;
/**
* DOM解析xml文件
* @author win
*
*/
public class DomDemo01 {
/**
* @param args
*/
public static void main(String[] args) {
//1.建立DocumentBuilderFactory,用于取得DocumentBuilder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//2.通过DocumentBuilderFactory取得DocumentBuilder
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
//3.定义Document接口对象,通过DocumentBuilder类进行DOM树的转换操作
Document doc = null;
try {
doc = builder.parse("D:"+File.separator+"dom_demo_02.xml");
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//4.查找name节点
NodeList nl = doc.getElementsByTagName("linkman");
//输出NodeList中第一个子节点中的文本内容
for(int x=0;x<nl.getLength();x++){
Element e = (Element) nl.item(x);
System.out.println("姓名:"+e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
System.out.println("姓名:"+e.getElementsByTagName("email").item(0).getFirstChild().getNodeValue());
}
//System.out.println("姓名:"+nl.item(0).getFirstChild().getNodeValue());
}
}
package com.hbut.xml; import java.io.File; 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; /** * 生成xml文件 * @author win * */ public class DomDemo03 { public static void main(String[] args) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder =factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); } Document doc = null; doc = builder.newDocument(); //建立各个节点 Element addresslist = doc.createElement("addresslist"); Element linkname = doc.createElement("linkname"); Element name = doc.createElement("name"); Element email = doc.createElement("email"); //设置各个操作节点,为节点添加文本 name.appendChild(doc.createTextNode("李兴华")); email.appendChild(doc.createTextNode("21223232@qq.com")); //设置节点关系 linkname.appendChild(name); linkname.appendChild(email); addresslist.appendChild(linkname); doc.appendChild(addresslist); //输出文档到文件中 TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = null; try { t = tf.newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } t.setOutputProperty(OutputKeys.ENCODING,"GBK"); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File("D:"+File.separator+"output.xml")); try { t.transform(source,result); } catch (TransformerException e) { e.printStackTrace(); } } }
原文链接:https://www.f2er.com/xml/300178.html