Ø
DOM
是一种用于
XML
文档对象模型,可用于直接访问
XML
文档的各个部位,在
DOM
中文档被模拟为树状,其中
XML
语法的每一个组成部分都表示一个节点,
DOM
允许用户遍历文档树,从父节点移动到子节点和兄弟节点。并利用某节点类型特有的属性
(
元素具有属性,文本节点具有文本数据
)
Ø
节点
(XML
文档中的每一个成分都是一个节点
)
Ø
DOM
是这样规定的:
Ø
整个文档是一个节点文档
Ø
每一个
XML
标签是一个元素节点
Ø
包含在
XML
元素中的文本是文本节点
Ø
每一个
XML
属性是一个属性节点
JDK中Dom主要以下列形式体现:
接口:
- org.w3c.dom.NamedNodeMap
- org.w3c.dom.NameList
- org.w3c.dom.Node
- org.w3c.dom.Attr
- org.w3c.dom.CharacterData
- org.w3c.dom.Comment
- org.w3c.dom.Text
- org.w3c.dom.CDATASection
- org.w3c.dom.Document
- org.w3c.dom.DocumentFragment
- org.w3c.dom.DocumentType
- org.w3c.dom.Element
- org.w3c.dom.Entity
- org.w3c.dom.EntityReference
- org.w3c.dom.Notation
- org.w3c.dom.ProcessingInstruction
- org.w3c.dom.NodeList
- NodeList getChildNodes()
- Node getParentNode()
- getFirstChild() getLastChild() getNextSibing() getPreviousSilbing()
- NamedNodeMap getAttrbutes()
- String getNodeName() String getNOdeValue()
NodeLIst NamedNodeMap接口:int getLength() Node item(int index)
CharacterData : String getData()
Document接口: Element getDocumentElement() 返回文档的根元素
Element : String getTagName() String getAttribute(String name )
在JDK中Parser主要有两个:
- java.lang.Object
- javax.xml.parsers.DocumentBuilder
- javax.xml.parsers.DocumentBuilderFactory
- javax.xml.parsers.SAXParser
- javax.xml.parsers.SAXParserFactory
类分层结构
- java.lang.Object
-
- javax.xml.xpath.XPathConstants
- javax.xml.xpath.XPathFactory
接口分层结构
- javax.xml.xpath.XPath
- javax.xml.xpath.XPathExpression
- javax.xml.xpath.XPathFunction
- javax.xml.xpath.XPathFunctionResolver
- javax.xml.xpath.XPathVariableResolver
package com.dom.xml;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DomParseService {
public DomParseService() {
// TODO Auto-generated constructor stub
}
public List<Book> getBooks(InputStream inputStream) throws Exception {
List<Book> list = new ArrayList<Book>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(inputStream);
Element element = document.getDocumentElement();
NodeList bookNodes = element.getElementsByTagName("book");
for (int i = 0; i < bookNodes.getLength(); i++) {
Element bookElement = (Element) bookNodes.item(i);
Book book = new Book();
book.setId(Integer.parseInt(bookElement.getAttribute("id")));
NodeList childNodes = bookElement.getChildNodes();
// System.out.println("*****"+childNodes.getLength());
for (int j = 0; j < childNodes.getLength(); j++) {
if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
if ("name".equals(childNodes.item(j).getNodeName())) {
book.setName(childNodes.item(j).getFirstChild()
.getNodeValue());
} else if ("price".equals(childNodes.item(j).getNodeName())) {
book.setPrice(Float.parseFloat(childNodes.item(j)
.getFirstChild().getNodeValue()));
}
}
}// end for j
list.add(book);
}// end for i
return list;
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String path = "http://localhost:8080/myhttp/books.xml";
InputStream input = HttpUtils.getXML(path);
DomParseService dom = new DomParseService();
List<Book> books = dom.getBooks(input);
for (Book book : books) {
System.out.println(book.toString());
}
}
}
几个类
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(inputStream);
Element element = document.getDocumentElement();
NodeList bookNodes = element.getElementsByTagName("book");
缺点:
必须知道文档的格式