标签:
2 DOM解析 (Document object Model)w3c 3
解析出的和原来的配置文件几乎一样
(1)需要继承DefaultHandler
(2)重写五个方法
Characters: 标签中的文本值
endDocument: 结束文档
endElement: 结束标签
startDocument: 文档开始解析
startElement: 开始标签 属性包含在开始标签里面
public class ParseBySAX extends DefaultHandler{
//标签中的文本值
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.print(new String(ch,start,length));
}
//结束文档
@Override
public void endDocument() throws SAXException {
System.out.print("文档结束解析");
}
//结束标签
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.print("<"+qName+">");
}
@Override
public void startDocument() throws SAXException {
System.out.println("文档开始解析");
}
//开始标签 属性包含在开始标签里面
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
String s = "<"+qName;
for(int i=0;i<attributes.getLength();i++){
String attrName = attributes.getQName(i);
String attrValue = attributes.getValue(i);
s+=" "+attrName+"=\""+attrValue+"\"";
}
s+=">";
System.out.print(s);
}
public static void main(String[] args) throws Exception, SAXException {
//创建解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
//获取解析器
SAXParser parse = factory.newSAXParser();
//开始解析
parse.parse(new File("src/book.xml"), new ParseBySAX());
}
}
文档开始解析
<books>
<book no="01011">
<name>还珠格格<name>
<author>琼瑶<author>
<publish id="1">湖南电视台<publish>
<price>3<price>
<book>
<book no="01022">
<name>神雕侠侣<name>
<author>包中<author>
<publish>湖南电视台<publish>
<price>22<price>
<book>
<book no="01033">
<name>一代枭雄<name>
<author>孙红雷<author>
<publish>山地<publish>
<price>123<price>
<book>
<books>文档结束解析
getDocumentElement():该属性允许直接访问文档的文档元素的子节点
getElementsByTagName(name):按文档顺序返回包含在文档中且具有给定标记名称的所有 Element 的 NodeList。
public void parse1(Document document){
//获取文档的根元素
Element root = document.getDocumentElement();
System.out.println("获取文件的根元素"+root.getNodeName());
//获取根元素的所有子元素
NodeList nodes = root.getChildNodes();
System.out.println("获取根元素的所有子元素的个数"+nodes.getLength());
for(int i=0;i<nodes.getLength();i++){
Node node = nodes.item(i);
//System.out.println(node.getNodeType());
//判断当前节点是否是一个完整的标签元素
if(node.getNodeType()==Node.ELEMENT_NODE){
//获取当前节点所有属性的集合
NamedNodeMap map = node.getAttributes();
Node n = map.getNamedItem("no");
System.out.println("n0: "+n.getNodeValue());
//获取book节点下的所有子节点
NodeList list = node.getChildNodes();
for(int j=0;j<list.getLength();j++){
Node n2 = list.item(j);
if(n2.getNodeType()==Node.ELEMENT_NODE){
//获得节点的内容
String text = n2.getTextContent();
System.out.println(n2.getNodeName()+": "+text);
}
}
System.out.println();
}
}
}
public void parse2(Document document){
NodeList list = document.getElementsByTagName("book");
for(int i=0;i<list.getLength();i++){
Node node = list.item(i);
String no = node.getAttributes().getNamedItem("no").getNodeValue();
System.out.println("no: "+no);
NodeList nodes = node.getChildNodes();
for(int j=0;j<nodes.getLength();j++){
if(nodes.item(j).getNodeType()==Node.ELEMENT_NODE){
System.out.println(nodes.item(j).getNodeName()+" : "+nodes.item(j).getTextContent());
}
}
System.out.println();
}
}
public void getNodes(Node n){
if(n.hasChildNodes()){
NodeList n1 = n.getChildNodes();
for(int i=0;i<n1.getLength();i++){
Node node = n1.item(i);
if(node.getNodeType()==Node.ELEMENT_NODE){
System.out.println(node.getNodeName()+" : "+node.getTextContent());
}
getNodes(node);
}
}
}
public static void main(String[] args) {
try {
//获得解析器工厂 用来生产解析器
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//获得解析器
DocumentBuilder builder = factory.newDocumentBuilder();
//解析指定源获得解析文档
Document document = builder.parse(new File("src/book.xml"));
System.out.println(document);
//new ParseByDom().parse1(document);
//new ParseByDom().parse2(document);
Node n = document.getDocumentElement();
new ParseByDom().getNodes(n);
} catch (Exception e) {
e.printStackTrace();
}
}
//获得解析器
SAXBuilder bulider = new SAXBuilder();
//解析文件获取文档对象
Document document = bulider.build(new File("src/book.xml"));
//获取文档根元素
Element root = document.getRootElement();
//获取指定所有的book节点
List list = root.getChildren("book");
for(Object obj:list){
Element ele = (Element)obj;
//获取当前节点中指定属性的值
String no = ele.getAttributeValue("no");
String name = ele.getChild("name").getText();
String author = ele.getChild("author").getText();
String publish = ele.getChild("name").getText();
String price = ele.getChild("price").getText();
System.out.println("no : "+no);
System.out.println("name : "+name);
System.out.println("author : "+author);
System.out.println("publish : "+publish);
System.out.println("price : "+price);
System.out.println();
}
public void parse1(Document document){
Element root = document.getRootElement();
//获取当前根元素下所有的book子元素
Iterator<Element> it = root.elementIterator("book");
while(it.hasNext()){
Element e = it.next();
//获取属性
String no = e.attributeValue("no");
//获取子节点
String name = e.element("name").getTextTrim();
String author = e.element("author").getTextTrim();
String publish = e.element("publish").getTextTrim();
String price = e.element("price").getTextTrim();
System.out.println("no : "+no);
System.out.println("name : "+name);
System.out.println("author : "+author);
System.out.println("publish : "+publish);
System.out.println("price : "+price);
System.out.println();
}
}
public void parse2(Document document){
//找寻大节点 里面有小的节点
List<Node> list = document.selectNodes("books/book");
for(Node n:list){
String no = n.valueOf("@no");
//获得单个的
String name = n.selectSingleNode("name").getName().trim();
String author= n.selectSingleNode("author").getName().trim();
String publish = n.selectSingleNode("publish").getName().trim();
String price = n.selectSingleNode("price").getName().trim();
System.out.println("no : "+no);
System.out.println("name : "+name);
System.out.println("author : "+author);
System.out.println("publish : "+publish);
System.out.println("price : "+price);
System.out.println();
}
}
public static void main(String[] args) throws Exception {
//获取解析器
SAXReader reader = new SAXReader();
//开始解析
Document document = reader.read(new File("src/book.xml"));
//new ParseByDOM4J().parse1(document);
new ParseByDOM4J().parse1(document);
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/yzjyhp/article/details/47046099