Java解析xml——Dom解析

来源:互联网 发布:蔡英文年金改革 知乎 编辑:程序博客网 时间:2024/05/17 00:05

一、Dom解析获取标签属性

books.xml:

<?xml version="1.0" encoding="UTF-8"?><bookstore><book id="1"><name>冰与火之歌</name><author>乔治马丁</author><time>2014</time><price>89</price></book><book id="2"><name>安徒生童话</name><price>40</price><time>2004</time><language>Englist</language></book></bookstore>


DomParseXml.java
public class DomParseXml {public static void main(String[] args) {//1、创建DocumentBuilderFactory对象DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();try {//2、创建DocumentBuilder对象DocumentBuilder db = dbf.newDocumentBuilder();//3、将books.xml文件加载到项目中Document document = db.parse("xml/books.xml");//4、获取所有的book节点NodeList nodeList = document.getElementsByTagName("book");System.out.println("==========共有" + nodeList.getLength() + "本书==========");for(int i = 0; i < nodeList.getLength(); i++) {//5、通过NodeList的item(int index)方法遍历每一个book节点Node node = nodeList.item(i);//6-1、在不知道属性有哪几个的情况下通过Node.getAttributes()方法获取所有的属性节点//然后再遍历每一个节点,获取节点名称(属性名)与节点值(属性值)NamedNodeMap attrs = node.getAttributes();System.out.println("=====第" + (i + 1) + "本书共有" + attrs.getLength() + "个属性=====");for(int j = 0; j < attrs.getLength(); j++) {Node attr = attrs.item(j);System.out.println("第" + (j + 1) + "个属性" + attr.getNodeName() + "--->" + attr.getNodeValue());}//6-2、在已知节点的属性情况下,可以在获取节点时强制转换为Element再使用getAttribute(String atrr)获取属性值Element attr = (Element) nodeList.item(i);System.out.println("已知有id属性,id的值为:" + attr.getAttribute("id"));}} catch (ParserConfigurationException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
运行结果:

==========共有2本书==========
=====第1本书共有1个属性=====
第1个属性id--->1
已知有id属性,id的值为:1
=====第2本书共有1个属性=====
第1个属性id--->2
已知有id属性,id的值为:2


二、Dom解析获取标签的值

books2.xml:

<?xml version="1.0" encoding="UTF-8"?><bookstore><book id="1"><name><country>美国</country>冰与火之歌</name><author>乔治马丁</author><time>2014</time><price>89</price></book></bookstore>

DomParseXml2.java:

public class DomParseXml2 {public static void main(String[] args) {DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();try {DocumentBuilder db = dbf.newDocumentBuilder();Document document = db.parse("xml/books2.xml");NodeList nodeList = document.getElementsByTagName("book");System.out.println("==========共有" + nodeList.getLength() + "本书==========");for(int i = 0; i < nodeList.getLength(); i++) {Node node = nodeList.item(i);//1、获取所有的子节点NodeList childList = node.getChildNodes();System.out.println("=========第" + (i + 1) +"本书有" + childList.getLength() + "个子节点");//2、遍历子节点for(int j = 0; j < childList.getLength(); j++) {Node child = childList.item(j);//3、Dom解析所有的空白换行也作为节点,这里需要进行过滤,只有是标签类型的节点才读取值if(child.getNodeType() == Node.ELEMENT_NODE) {//4、获取节点的名称System.out.println("======第" + (j + 1) + "个子节点名为" + child.getNodeName() + "=====");//5、Element类型的节点不能通过Node.getNodeValue()获取节点的值,这样获取只能返回nullSystem.out.println("通过Node.getNodeValue()方法获取节点的值-->" + child.getNodeValue());//6、通过Node.getFirstChild().getNodeValue()获取节点的值,如果子节点不是Text类型的,则返回nullSystem.out.println("通过Node.getFirstChild().getNodeValue()方法获取节点的值-->" + child.getFirstChild().getNodeValue());//7、通过Node.getTextContent()获取节点的值,这个方法会获取所有的子节点的值并依次排列System.out.println("通过Node.getTextContent()方法获取节点的值-->" + child.getTextContent());}}}} catch (ParserConfigurationException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}
运行结果:

==========共有1本书==========
=========第1本书有9个子节点
======第2个子节点名为name=====
通过Node.getNodeValue()方法获取节点的值-->null
通过Node.getFirstChild().getNodeValue()方法获取节点的值-->null
通过Node.getTextContent()方法获取节点的值-->美国冰与火之歌
======第4个子节点名为author=====
通过Node.getNodeValue()方法获取节点的值-->null
通过Node.getFirstChild().getNodeValue()方法获取节点的值-->乔治马丁
通过Node.getTextContent()方法获取节点的值-->乔治马丁
======第6个子节点名为time=====
通过Node.getNodeValue()方法获取节点的值-->null
通过Node.getFirstChild().getNodeValue()方法获取节点的值-->2014
通过Node.getTextContent()方法获取节点的值-->2014
======第8个子节点名为price=====
通过Node.getNodeValue()方法获取节点的值-->null
通过Node.getFirstChild().getNodeValue()方法获取节点的值-->89
通过Node.getTextContent()方法获取节点的值-->89


0 0
原创粉丝点击