`
swerit
  • 浏览: 57866 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

java-DOM解析xml文档

    博客分类:
  • j2se
xml 
阅读更多
xml文档中的常见节点:




import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.*;


public class code10_3 {
	static Document document;
	
	public static void main(String args[]){
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try{
			DocumentBuilder db = dbf.newDocumentBuilder();
			//读取xml文档
			document = db.parse(new File("G:\\EclipseWorkPlace\\test.xml"));
			//获得根元素
			Node root = document.getDocumentElement();
			System.out.println("根节点的名字是:" + root.getNodeName());
			//获得根元素的子节点列表
			NodeList childs = root.getChildNodes();
			System.out.println("子节点的长度是:" + childs.getLength());
			getElement(childs);
		}catch (Exception e){
			System.out.println(e);
		}
	}
	
	public static void getElement(NodeList childs){
		int i=0;
		if (childs.getLength() == 0){
			//该孩子没有子节点
			System.out.println("该孩子没有子节点!");
			return;
		}
		
		for (i=0; i<childs.getLength(); i++){
			//取得第i个子节点
			Node node = childs.item(i);
			//获取节点的类型,可以是ElementNode,TextNode,DocumentNode等
			short nodetype = node.getNodeType();
			/*ElementNode类型的节点可以包含子节点和属性等*/
			if (nodetype == Node.ELEMENT_NODE){
				//得到节点的名称
				String name = node.getNodeName();
				String attrValue = "", attrName = "";
				System.out.println("This is element! name is:" + name);
				if (node.hasAttributes()){
					
					NamedNodeMap attrlist = node.getAttributes();
					for (int j=0; j<attrlist.getLength(); j++){
						Node attrNode = attrlist.item(j);
						attrName = attrNode.getNodeName();
						attrValue = attrNode.getNodeValue();
						System.out.println("this element attr is:"+
						attrValue + ";attrname is :" + attrName);
					}
				}
				
				//如有子节点,递归调用getElement()
				if (node.hasChildNodes()){
					getElement(node.getChildNodes());
				}
			}
			
			/*Text类型节点没有子节点,节点名为#text,节点的值为XML文档中的元素值*/
			if (nodetype == Node.TEXT_NODE){
				//该节点的name是“#text”
				String txtName = node.getNodeName();
				//取出Text类型节点的值
				Node thisparent = node.getParentNode();
				Node txtNode = thisparent.getChildNodes().item(0);
				String txtValue = txtNode.getNodeValue();
				if (txtValue.trim().length() > 0){
					System.out.println("txtName=" + txtName +
							"; txtValue = " + txtValue.trim());
				}
			}
		}
	}
}


<?xml version="1.0" encoding='utf-8'?>
<completedRequest currentTime='' startDay='2009-12-23'
	endDay='2009-12-23'>
	<building buildingName='1'>
		<floor floorName='1'>
			<line lineName='N1'>
				<completedInfo seatNo='11' responseTime='11:25'
					RepairTime='11:23'>
					<completedDetail requestTime='11:23'
						requestStaffNo='H0050' requestStaffName='张三' responseTime='11:25'
						responseStaffNo='J332' responseStaffName='李四' completeTime='11:50'
						completeStaffNo='Y6500' completeStaffName='梁五'>
					</completedDetail>
				</completedInfo>
				
				<completedInfo seatNo='12' responseTime='11:25'
					RepairTime='11:23'>
					<completedDetail requestTime='11:23'
						requestStaffNo='H00501' requestStaffName='张三1' responseTime='11:25'
						responseStaffNo='J3321' responseStaffName='李四1' completeTime='11:50'
						completeStaffNo='Y65001' completeStaffName='梁五1'>
					</completedDetail>
				</completedInfo>
			</line>
		</floor>
	</building>
	<building buildingName='1'>
		<floor floorName='1'>
			<line lineName='N1'>
				<completedInfo seatNo='11' responseTime='11:25'
					RepairTime='11:23'>
					<completedDetail requestTime='11:23'
						requestStaffNo='H0050' requestStaffName='张三' responseTime='11:25'
						responseStaffNo='J332' responseStaffName='李四' completeTime='11:50'
						completeStaffNo='Y6500' completeStaffName='梁五'>
					</completedDetail>
				</completedInfo>
				
				<completedInfo seatNo='12' responseTime='11:25'
					RepairTime='11:23'>
					<completedDetail requestTime='11:23'
						requestStaffNo='H00501' requestStaffName='张三1' responseTime='11:25'
						responseStaffNo='J3321' responseStaffName='李四1' completeTime='11:50'
						completeStaffNo='Y65001' completeStaffName='梁五1'>
					</completedDetail>
				</completedInfo>
			</line>
		</floor>
	</building>
	
	<building buildingName='1'>
		<floor floorName='1'>
			<line lineName='N1'>
				<completedInfo seatNo='11' responseTime='11:25'
					RepairTime='11:23'>
					<completedDetail requestTime='11:23'
						requestStaffNo='H0050' requestStaffName='张三' responseTime='11:25'
						responseStaffNo='J332' responseStaffName='李四' completeTime='11:50'
						completeStaffNo='Y6500' completeStaffName='梁五'>
					</completedDetail>
				</completedInfo>
				
				<completedInfo seatNo='12' responseTime='11:25'
					RepairTime='11:23'>
					<completedDetail requestTime='11:23'
						requestStaffNo='H00501' requestStaffName='张三1' responseTime='11:25'
						responseStaffNo='J3321' responseStaffName='李四1' completeTime='11:50'
						completeStaffNo='Y65001' completeStaffName='梁五1'>
					</completedDetail>
				</completedInfo>
			</line>
		</floor>
	</building>
	
</completedRequest>


结果:
=============================================================
根节点的名字是:completedRequest
子节点的长度是:7
This is element! name is:building
this element attr is:1;attrname is :buildingName
This is element! name is:floor
this element attr is:1;attrname is :floorName
This is element! name is:line
this element attr is:N1;attrname is :lineName
This is element! name is:completedInfo
this element attr is:11:23;attrname is :RepairTime
this element attr is:11:25;attrname is :responseTime
this element attr is:11;attrname is :seatNo
This is element! name is:completedDetail
this element attr is:梁五;attrname is :completeStaffName
this element attr is:Y6500;attrname is :completeStaffNo
this element attr is:11:50;attrname is :completeTime
this element attr is:张三;attrname is :requestStaffName
this element attr is:H0050;attrname is :requestStaffNo
this element attr is:11:23;attrname is :requestTime
this element attr is:李四;attrname is :responseStaffName
this element attr is:J332;attrname is :responseStaffNo
this element attr is:11:25;attrname is :responseTime
This is element! name is:completedInfo
this element attr is:11:23;attrname is :RepairTime
this element attr is:11:25;attrname is :responseTime
this element attr is:12;attrname is :seatNo
This is element! name is:completedDetail
this element attr is:梁五1;attrname is :completeStaffName
this element attr is:Y65001;attrname is :completeStaffNo
this element attr is:11:50;attrname is :completeTime
this element attr is:张三1;attrname is :requestStaffName
this element attr is:H00501;attrname is :requestStaffNo
this element attr is:11:23;attrname is :requestTime
this element attr is:李四1;attrname is :responseStaffName
this element attr is:J3321;attrname is :responseStaffNo
this element attr is:11:25;attrname is :responseTime
This is element! name is:building
this element attr is:1;attrname is :buildingName
This is element! name is:floor
this element attr is:1;attrname is :floorName
This is element! name is:line
this element attr is:N1;attrname is :lineName
This is element! name is:completedInfo
this element attr is:11:23;attrname is :RepairTime
this element attr is:11:25;attrname is :responseTime
this element attr is:11;attrname is :seatNo
This is element! name is:completedDetail
this element attr is:梁五;attrname is :completeStaffName
this element attr is:Y6500;attrname is :completeStaffNo
this element attr is:11:50;attrname is :completeTime
this element attr is:张三;attrname is :requestStaffName
this element attr is:H0050;attrname is :requestStaffNo
this element attr is:11:23;attrname is :requestTime
this element attr is:李四;attrname is :responseStaffName
this element attr is:J332;attrname is :responseStaffNo
this element attr is:11:25;attrname is :responseTime
This is element! name is:completedInfo
this element attr is:11:23;attrname is :RepairTime
this element attr is:11:25;attrname is :responseTime
this element attr is:12;attrname is :seatNo
This is element! name is:completedDetail
this element attr is:梁五1;attrname is :completeStaffName
this element attr is:Y65001;attrname is :completeStaffNo
this element attr is:11:50;attrname is :completeTime
this element attr is:张三1;attrname is :requestStaffName
this element attr is:H00501;attrname is :requestStaffNo
this element attr is:11:23;attrname is :requestTime
this element attr is:李四1;attrname is :responseStaffName
this element attr is:J3321;attrname is :responseStaffNo
this element attr is:11:25;attrname is :responseTime
This is element! name is:building
this element attr is:1;attrname is :buildingName
This is element! name is:floor
this element attr is:1;attrname is :floorName
This is element! name is:line
this element attr is:N1;attrname is :lineName
This is element! name is:completedInfo
this element attr is:11:23;attrname is :RepairTime
this element attr is:11:25;attrname is :responseTime
this element attr is:11;attrname is :seatNo
This is element! name is:completedDetail
this element attr is:梁五;attrname is :completeStaffName
this element attr is:Y6500;attrname is :completeStaffNo
this element attr is:11:50;attrname is :completeTime
this element attr is:张三;attrname is :requestStaffName
this element attr is:H0050;attrname is :requestStaffNo
this element attr is:11:23;attrname is :requestTime
this element attr is:李四;attrname is :responseStaffName
this element attr is:J332;attrname is :responseStaffNo
this element attr is:11:25;attrname is :responseTime
This is element! name is:completedInfo
this element attr is:11:23;attrname is :RepairTime
this element attr is:11:25;attrname is :responseTime
this element attr is:12;attrname is :seatNo
This is element! name is:completedDetail
this element attr is:梁五1;attrname is :completeStaffName
this element attr is:Y65001;attrname is :completeStaffNo
this element attr is:11:50;attrname is :completeTime
this element attr is:张三1;attrname is :requestStaffName
this element attr is:H00501;attrname is :requestStaffNo
this element attr is:11:23;attrname is :requestTime
this element attr is:李四1;attrname is :responseStaffName
this element attr is:J3321;attrname is :responseStaffNo
this element attr is:11:25;attrname is :responseTime
===============================================================
  • 大小: 13.1 KB
0
3
分享到:
评论

相关推荐

    java_dom解析xml xml java

    标题“java_dom解析xml xml java”表明了本文档的主题是关于如何使用Java中的DOM技术来解析XML文件。 #### 描述分析 描述中提到这是一个适合新手入门的内容,并给出了一个简单的XML示例。该XML文档包含了一个`...

    java dom 解析 xml 实例

    Java DOM 解析 XML 实例是 Java 语言中常用的 XML 解析方法之一,使用 W3C 推荐的文档对象模型(Document Object Model,DOM)来解析 XML 文档。DOM 提供了一个树形结构的对象模型,通过遍历树形结构可以访问和操作 ...

    DOM_XML.rar_DOM_dom xml_dom xml java_dom解析xml_java解析xml

    学习DOM解析XML对于任何需要处理XML数据的Java开发者都是基础且重要的技能,因为它提供了灵活性和强大的功能来创建、修改和分析XML文档。然而,DOM的缺点是内存占用大,对于大型XML文档,可能需要考虑更轻量级的解析...

    JAVA 解析XML生成XML文档实例

    JAVA 解析XML和生成XML文档源码。比较全 1.DOM生成和解析XML文档 2.SAX生成和解析XML文档 3.DOM4J生成和解析XML文档 4.JDOM生成和解析XML

    java中用dom解析xml的经典入门级文档

    #### 四、使用DOM解析XML文档 接下来,我们通过一个简单的例子来看一下如何使用DOM来解析XML文档。 ##### 1. 示例XML文档 ```xml &lt;?xml version="1.0" encoding="gb2312"?&gt; &lt;name&gt;rjzjh &lt;price&gt;jjjjjj ``...

    java使用dom解析xml

    Java 使用 DOM 解析 XML 是一种常见的处理 XML 文档的方式,DOM 即 Document Object Model,它是一种 W3C 标准的 API,用于处理 XML 和 HTML 文档。在 Java 中,DOM 解析允许开发者将整个 XML 文件加载到内存中,...

    Android解析xml(2)---DOM解析

    以下是关于Android中DOM解析XML的详细知识讲解。 1. **DOM解析器简介** DOM解析器是XML解析的一种方法,它将整个XML文档加载到内存中的一个对象模型,这个模型称为DOM树。开发者可以遍历这个树来访问或修改XML文件...

    java平台中使用DOM解析xml文件

    总的来说,理解并熟练掌握DOM解析XML是Java开发中的基础技能,无论是在简单的数据读取还是复杂的XML操作中,它都能提供强大的支持。同时,根据项目需求和性能考虑,可以选择JDOM等其他XML处理库作为替代方案。

    Java使用sax、dom、dom4j解析xml文档

    Java提供了多种解析XML的API,包括SAX(Simple API for XML)、DOM(Document Object Model)以及DOM4J。下面我们将详细探讨这些解析方式及其在实际开发中的应用。 1. SAX解析器: SAX是一种基于事件驱动的解析器,...

    dom4j---xml解析jar包

    在Java环境中,解析XML文档时,我们通常会遇到各种库,其中DOM4J是一个非常流行的、功能强大的开放源代码库。这个库不仅简化了XML处理,还提供了XPath和XSLT的支持,使其成为Java开发者处理XML的首选工具之一。 **...

    JAVA -XML解析总结

    1. DOM解析: DOM 是一种将 XML 文档转化为对象树的模型,它将整个 XML 文件加载到内存中,以便可以随机访问任何部分。DOM 解析器首先读取 XML 文件,然后构建一个由节点组成的树形结构,每个节点代表 XML 文档的一...

    dom4j---xml解析包

    1. DOM解析:DOM是一种将整个XML文档加载到内存中的模型,构建一个树形结构,允许开发者通过节点遍历、查找和修改XML内容。DOM4J的DOM解析器提供了一套完整的API,可以方便地创建、查询和修改XML文档。这种方法适合...

    JAVA DOM解析XML文件过程详解

    JAVA DOM 解析 XML 文件过程详解 JAVA DOM 解析 XML 文件过程详解是指使用 JAVA 语言通过 DOM(Document Object Model)解析 XML 文件的过程。DOM 是一种应用程序接口(API),它将 XML 文档转换为一个树形结构,...

    java中dom解析xml

    总结起来,Java中的DOM解析XML是一种强大的技术,允许程序员以对象形式处理XML文档,通过构建的树形结构可以方便地进行读写操作。然而,对于大规模的XML文件,应考虑其他低内存占用的解析策略。在实际项目中,开发者...

    (java)DOM解析XML文件并实现通讯录

    在Java编程语言中,DOM(Document Object Model)是一种解析XML文档的标准方法,它将XML文件转化为一个树形结构,使得开发者可以方便地访问和操作XML文档的每一个元素。本项目"DOM解析XML文件并实现通讯录"就是利用...

    DOM解析XML应用实例(入门经典案例)

    DOM解析XML时,会将整个XML文档加载到内存中,形成一棵由节点构成的树形结构,每个节点代表XML文档的一部分,如元素、属性、文本等。 首先,了解DOM解析的基本步骤: 1. 加载XML文档:使用Java的`...

    JAVA_dom解析xml

    Java DOM(Document Object Model)解析XML是一种...总的来说,DOM解析XML是Java中处理XML数据的基础技术,适用于小型或中型XML文档的读取和操作。理解并熟练掌握DOM解析,能够帮助开发者更高效地与XML数据进行交互。

    java实现xml解析word文档代码实现

    Java 实现 XML 解析 Word 文档代码实现 基于给定的文件信息,我们可以总结出以下知识点: 1. Java 实现 XML 解析 Word 文档 Java 可以使用多种方式来解析 Word 文档,包括使用 Apache POI 库、Java2Word 库等。...

Global site tag (gtag.js) - Google Analytics