DOM解析XML
解析類:
package tt;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 TestURL {
/**
* @param args
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
static ArrayList list = new ArrayList();
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
// TODO Auto-generated method stub
String abc="abc.xml";
URL url = TestURL.class.getClassLoader().getResource(abc);
String filePath = url.getPath();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(filePath));
Element element = doc.getDocumentElement();
NodeList nodeList = element.getChildNodes();
System.out.println(getAllSubNodesName(nodeList));
}
public static List getAllSubNodesName(NodeList nodeList){
for(int i=0;i <nodeList.getLength();i++){
Node node = nodeList.item(i);
if(node.getChildNodes().getLength() <1){
System.out.println(node.getNodeValue());
list.add(node.getNodeValue());
}
getAllSubNodesName(node.getChildNodes());
}
System.out.println(list.size());
return list;
}
}
xml格式:
<?xml version="1.0" encoding="UTF-8"?>
<all> <book> <title>the mythical man-month </title> <writer>frederick p.brooks Jr. </writer> <publishdate>1975-03-12 </publishdate> </book> <book> <title>the mythical man-month </title> <writer>frederick p.brooks Jr. </writer> <publishdate/> </book> </all>
輸出結果:
the mythical man-month
1
1
frederick p.brooks Jr.
2
2
1975-03-12
3
3
3the mythical man-month
4
4
frederick p.brooks Jr.
5
5
null
6
6
6
package tt;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 TestURL {
/**
* @param args
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
static ArrayList list = new ArrayList();
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
// TODO Auto-generated method stub
String abc="abc.xml";
URL url = TestURL.class.getClassLoader().getResource(abc);
String filePath = url.getPath();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(filePath));
Element element = doc.getDocumentElement();
NodeList nodeList = element.getChildNodes();
System.out.println(getAllSubNodesName(nodeList));
}
public static List getAllSubNodesName(NodeList nodeList){
for(int i=0;i <nodeList.getLength();i++){
Node node = nodeList.item(i);
if(node.getChildNodes().getLength() <1){
System.out.println(node.getNodeValue());
list.add(node.getNodeValue());
}
getAllSubNodesName(node.getChildNodes());
}
System.out.println(list.size());
return list;
}
}
xml格式:
<?xml version="1.0" encoding="UTF-8"?>
<all> <book> <title>the mythical man-month </title> <writer>frederick p.brooks Jr. </writer> <publishdate>1975-03-12 </publishdate> </book> <book> <title>the mythical man-month </title> <writer>frederick p.brooks Jr. </writer> <publishdate/> </book> </all>
輸出結果:
the mythical man-month
1
1
frederick p.brooks Jr.
2
2
1975-03-12
3
3
3the mythical man-month
4
4
frederick p.brooks Jr.
5
5
null
6
6
6
[the mythical man-month, frederick p.brooks Jr., 1975-03-12, the mythical man-month, frederick p.brooks Jr., null]
為什麼輸入數字那麼多次?為什麼不應該是一次6.
相關文章
- Python XML解析之DOMPythonXML
- XML DOM 解析器概述XML
- XML 檔案解析實踐 (DOM 解析)XML
- XML DOM 解析器錯誤概述XML
- 基於 DOM 的 XML 檔案解析類XML
- java的XML解析(DOM4J技術)JavaXML
- DOM4J 解析 XML 之忽略轉義字元XML字元
- 使用JAXP對xml文件進行DOM解析基礎XML
- XML DOM(Document Object Model)XMLObject
- 使用jaxp解析器dom方式對xml節點進行操作XML
- XML DOM Parse Error 物件概述XMLError物件
- PHP透過DOM操作XMLPHPXML
- XML DOM 新增節點概述XML
- XML和Dom4jXML
- Xml解析XML
- XML DOM – 屬性和方法概述XML
- XML DOM 載入函式概述XML函式
- XML DOM 替換節點概述XML
- XML DOM 刪除節點概述XML
- Java解析XMLJavaXML
- go 解析xmlGoXML
- iOS – XML解析iOSXML
- XML DOM 瀏覽器差異概述XML瀏覽器
- DOM 常用 API 解析API
- dom4j遍歷巢狀xml巢狀XML
- python之XML解析PythonXML
- 175.XML解析XML
- C# XML解析C#XML
- dom解析和sax解析的區別
- python XML 檔案解析PythonXML
- jdom解析xml檔案XML
- XML解析-最快的方式SAXXML
- 使用DocumentBuilderFactory解析XML淺談UIXML
- 使用 Java 解析XML檔案JavaXML
- React-原始碼解析-DOM模型React原始碼模型
- Python解析XML檔案生成HTMLPythonXMLHTML
- 搞懂 XML 解析,徒手造 WEB 框架XMLWeb框架
- 【spring原始碼系列】之【xml解析】Spring原始碼XML
- NPM酷庫051:xml2js,解析XML格式資料NPMXMLJS