Java学习之路——使用DOM解析XML文档

第一种方式:使用dom获取属性的值和文本的值进行解析xml

package com.lcq.java.document; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class DomTest1 { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { //第一步:获得dom解析工厂 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //第二部:获得dom解析器 DocumentBuilder db = dbf.newDocumentBuilder(); //第三部:解析一个xml文档,获得Document对象(根节点) Document document = db.parse(new File("test.xml")); System.out.println(document.getXmlEncoding()); System.out.println(document.getXmlVersion()); System.out.println(document.getXmlStandalone()); NodeList nodeList = document.getElementsByTagName("resourceitem"); System.out.println(nodeList.getLength()); for(int i = 0; i < nodeList.getLength(); i++){ Element element = (Element)nodeList.item(i); String title = element.getElementsByTagName("title").item(0).getFirstChild().getNodeValue(); System.out.println("title :" + title); String keywords = element.getElementsByTagName("keywords").item(0).getFirstChild().getNodeValue(); System.out.println("keywords :" + keywords); String kind = element.getElementsByTagName("kind").item(0).getFirstChild().getNodeValue(); System.out.println("kind :" + kind); String describe = element.getElementsByTagName("describe").item(0).getFirstChild().getNodeValue(); System.out.println("describe :" + describe); String date = element.getElementsByTagName("date").item(0).getFirstChild().getNodeValue(); System.out.println("date :" + date); System.out.println("------------------------------------------"); } } }

 

运行结果:


第二种方式:运用递归方法解析一般的xml文档

package com.lcq.java.document; /** * * 功能:运用递归方法解析一般的xml文档 * */ import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Attr; import org.w3c.dom.Comment; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class DomTest3 { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { //第一步:获得dom解析工厂 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //第二部:获得dom解析器 DocumentBuilder db = dbf.newDocumentBuilder(); //第三部:解析一个xml文档,获得Document对象(根节点) Document document = db.parse(new File("test.xml")); //获得根元素结点 Element root = document.getDocumentElement(); //调用递归函数,打印xml的内容 parseElement(root); } private static void parseElement(Element element){ String tagName = element.getNodeName(); NodeList children = element.getChildNodes(); System.out.print("<" + tagName); //element 元素所构成的属性的NamedNodeMap对象,对其进行判断 NamedNodeMap map = element.getAttributes(); if(null != map){ for(int i = 0 ; i < map.getLength(); i++){ //获取元素的每一个属性 Attr attr = (Attr)map.item(i); String attrName = attr.getName(); String attrValue = attr.getValue(); System.out.print(" " + attrName + "=\"" + attrValue + "\"" ); } } System.out.print(">"); for(int i = 0; i < children.getLength(); i++ ){ Node node = children.item(i); Short nodeType = node.getNodeType(); //是元素类性,进行递归 if(nodeType == Node.ELEMENT_NODE){ parseElement((Element)node); } //是文本类性,打印出来 else if(nodeType ==Node.TEXT_NODE){ System.out.print(node.getNodeValue()); } //是注释,进行打印 else if(nodeType ==Node.COMMENT_NODE){ Comment comment = (Comment)node; String data = comment.getData(); System.out.print("<!--" + data + "-->"); } } System.out.println("</" + tagName + ">"); } }

运行结果:

转载于:https://www.cnblogs.com/lcqBlogs/archive/2011/10/23/2392380.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值