xml檔案的三種解析dom,SAX和dom4j
阿新 • • 發佈:2019-02-18
目錄
一、XML檔案
student.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- 內嵌 --> <!DOCTYPE students [ <!ELEMENT students (student*)> <!ELEMENT student (name,age,gender)> <!ELEMENT name (#PCDATA)> <!ELEMENT age (#PCDATA)> <!ELEMENT gender (#PCDATA)> <!ATTLIST student id ID #REQUIRED sex (male|female) #REQUIRED pid IDREFS #REQUIRED > ] > <students> <student id="a1" sex="male" pid="a1 a2"> <name>Tom</name> <age>20</age> <gender>male</gender> </student> <student id="a2" sex="male" pid="a1"> <name>Bob</name> <age>20</age> <gender>male</gender> </student> </students>
二、DOM解析
/** * 使用dom解析xml檔案 * * @param path xml檔案路徑 * * @throws Exception */ public static void domParse(String path) throws Exception { // 1.建立解析器的工廠類 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // 2.建立解析器物件 DocumentBuilder documentBuilder = factory.newDocumentBuilder(); // 3.解析檔案獲取文件物件 Document document = documentBuilder.parse(path); // 4.獲取文件根節點 Element root = document.getDocumentElement(); System.out.println("<" + root.getNodeName() + ">"); // 5.獲取根節點下面的所有子節點 NodeList firstList = root.getChildNodes(); for (int i = 0; i < firstList.getLength(); i++) { // node對應每一個節點,一級節點,下面包含很多二級節點 Node node = firstList.item(i); // System.out.println("一級節點:"+node.getNodeName()); // 取每一個結點的屬性值 // NamedNodeMap attributes = node.getAttributes(); // if (attributes != null && attributes.getLength() != 0) { // for (int j = 0; j < attributes.getLength(); j++) { // // 對於每個屬性 // Node item = attributes.item(j); // // System.out.println(item.getNodeName() +"----"+ item.getNodeType()); // } // } System.out.println("\t<" + node.getNodeName() + " " + node.getAttributes().item(0).getNodeName() + "='" + node.getAttributes().item(0).getTextContent() + "' " + node.getAttributes().item(1).getNodeName() + "='" + node.getAttributes().item(1).getTextContent() + "' " + node.getAttributes().item(2).getNodeName() + "='" + node.getAttributes().item(2).getTextContent() + "'>"); // 取出所有二級節點 NodeList secondList = node.getChildNodes(); for (int j = 0; j < secondList.getLength(); j++) { // 取出每一個二級節點 Node item = secondList.item(j); // System.out.println("二級節點:"+item.getNodeName()); System.out.println( "\t\t<" + item.getNodeName() + ">" + item.getTextContent() + "</" + item.getNodeName() + ">"); } System.out.println("\t</" + node.getNodeName() + ">"); } System.out.println("</" + root.getNodeName() + ">"); }
/** * 寫xml格式的內容到檔案 * * @param path * 檔案路徑 * @throws Exception **/ public static void domWriter(String path) throws Exception { // 1.建立解析器物件工廠 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); // 2.建立解析器物件 DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); // 3.構建文件物件 Document dom = documentBuilder.newDocument(); // 4.建立根節點 Element root = dom.createElement("students"); // 5.建立其他節點 Element firstNode = dom.createElement("student"); // 二級節點:name:Tom Element nameNode = dom.createElement("name"); nameNode.setTextContent("Tom"); // 二級節點:age:20 Element ageNode = dom.createElement("age"); ageNode.setTextContent("20"); // 二級節點:gender:male Element genderNode = dom.createElement("gender"); genderNode.setTextContent("male"); Element secondNode = dom.createElement("student"); // 二級節點:name:Bob Element nameNode1 = dom.createElement("name"); nameNode1.setTextContent("Bob"); // 二級節點:age:18 Element ageNode1 = dom.createElement("age"); ageNode1.setTextContent("18"); // 二級節點:gender:female Element genderNode1 = dom.createElement("gender"); genderNode1.setTextContent("female"); // 進行結點關聯 // 關聯根節點 dom.appendChild(root); // 關聯一級節點 root.appendChild(firstNode); root.appendChild(secondNode); // 關聯二級節點 firstNode.appendChild(nameNode); firstNode.appendChild(ageNode); firstNode.appendChild(genderNode); secondNode.appendChild(nameNode1); secondNode.appendChild(ageNode1); secondNode.appendChild(genderNode1); //建立轉化器物件工廠 TransformerFactory factory = TransformerFactory.newInstance(); //建立轉化器物件 Transformer transformer = factory.newTransformer(); //執行轉化方法 transformer.transform(new DOMSource(dom), new StreamResult(new File(path))); }
三、SAX解析
/**
* SAX解析xml檔案的方法
*
* @param path
* 需要解析的xml檔案的路徑
* @throws Exception
*/
public static void saxParse(String path) throws Exception {
//1.構建SAX解析器工廠
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
//2.構建解析器物件
SAXParser saxParser = saxParserFactory.newSAXParser();
//3.呼叫方法解析xml檔案
//parse(檔案物件(或流),自定義事件處理器)
// saxParser.parse(new FileInputStream(path), dh);
saxParser.parse(new File(path),new MyHandler());
}
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyHandler extends DefaultHandler {
/**
* 開始讀取文件
*/
@Override
public void startDocument() throws SAXException {
// System.out.println("開始讀取文件:startDocument()");
}
/**
* 文件讀取結束
*/
@Override
public void endDocument() throws SAXException {
// System.out.println("文件讀取結束:endDocument()");
}
/**
* 開始讀取元素時呼叫
* @param uri
* @param localName
* @param qName
* @throws SAXException
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
// System.out.println("開始讀取元素:startElement()");
// System.out.println(uri);
// System.out.println(localName);
// System.out.println(qName);
// System.out.println(attributes.getIndex(qName));
//取屬性
/*for (int i = 0; i < attributes.getLength(); i++) {
String qName2 = attributes.getQName(i);
String value = attributes.getValue(i);
System.out.println(qName2+"--"+value);
}*/
System.out.print("<"+qName);
for (int i = 0; i < attributes.getLength(); i++) {
String qName2 = attributes.getQName(i);
String value = attributes.getValue(i);
System.out.print(" "+qName2+"=\""+value+"\"");
}
System.out.print(">");
}
/**
* 結束讀取元素
*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
// System.out.println("結束讀取元素:endElement()");
// System.out.println(qName);
System.out.println("</"+qName+">");
}
/**
* 讀取到元素值時
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
// System.out.println("讀取到元素值時:characters()");
for (int i = 0; i < ch.length; i++) {
//元素值:[start,start+length)
if(i>=start&&i<start+length) {
System.out.print(ch[i]);//結果:Tom20maleBob20male
}
}
}
}
四、dom4j解析
/**
* dom4j解析xml檔案
*
* @param path
* 需要解析的xml檔案
* @throws Exception
*/
public static void dom4j(String path) throws Exception {
// System.out.println(path);
// 1.構建解析器
SAXReader sr = new SAXReader();
// 2.呼叫方法解析檔案
Document document = sr.read(new File(path));
// 3.獲取根節點
Element root = document.getRootElement();
System.out.println("<" + root.getName() + ">");
// 4.獲取一級節點集合,一級節點下面包含很多二級節點
List<Element> elements = root.elements();
for (Element element : elements) {
System.out.print("\t" + "<" + element.getName() + " ");
// 獲取一級節點屬性
List<Attribute> attr = element.attributes();
for (Attribute attribute : attr) {
System.out.print(attribute.getName() + "=\"" + attribute.getText() + " ");
}
System.out.println(">");
// 獲取二級節點
List<Element> e = element.elements();
for (Element e1 : e) {
System.out.println("\t\t" + "<" + e1.getName() + ">" + e1.getText() + "</" + e1.getName() + ">");
}
// 一級節點結束標籤
System.out.println("\t" + "</" + element.getName() + ">");
}
System.out.println("</" + root.getName() + ">");
}
/**
* dom4j寫資料到xml檔案
*
* @param xmlPath
* xml檔案路徑
* @throws Exception
*/
public static void dom4jWriter(String xmlPath) throws Exception {
// 1.建立一個document物件
Document document = DocumentHelper.createDocument();
// 2.建立根結點
Element root = document.addElement("employees");
// 3.建立一級節點
Element firstNode1 = root.addElement("employee");
Element firstNode2 = root.addElement("employee");
// 建立一級標籤屬性
firstNode1.setAttributeValue("status", "在職");
firstNode2.setAttributeValue("status", "離職");
// 4.建立二級節點
Element idNode1 = firstNode1.addElement("id");
Element nameNode1 = firstNode1.addElement("name");
Element ageNode1 = firstNode1.addElement("age");
Element depNameNode1 = firstNode1.addElement("depName");
Element idNode2 = firstNode2.addElement("id");
Element nameNode2 = firstNode2.addElement("name");
Element ageNode2 = firstNode2.addElement("age");
Element depNameNode2 = firstNode2.addElement("depName");
// 設定二級節點的值
idNode1.setText("A12");
nameNode1.setText("張三");
ageNode1.setText("18");
depNameNode1.setText("教學部");
idNode2.setText("A13");
nameNode2.setText("李四");
ageNode2.setText("23");
depNameNode2.setText("MIS部");
// 5.寫到檔案
// 格式化物件
OutputFormat format = OutputFormat.createPrettyPrint();
File file = new File(xmlPath);
if (!file.exists()) {
file.createNewFile();
}
// 格式化
// XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(file),format);
// 非格式化
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(file));
xmlWriter.write(document);
xmlWriter.flush();
xmlWriter.close();
}