Java解析XML文档方法笔记

我们要处理XML文档,就要先解析(parse)它。在Java当中如何解析(parse)这类XML文档呢?

Java库提供了两种XML解析器:

1.像文档对象模型(Document Object Model,DOM)解析器这样的树型解析器(tree parse),它们将读入的XML文档转换成树结构

2.像XML简单API(Simple API for XML,SAX)解析器这样的流机制解析器(streaming parse),它们在读入XML文档时生成相应的事件。

两种XML解析器之间的选择:如果你要处理很长的文档,用它生成树结构将会消耗大量的内存空间,或者你只对某些元素感兴趣而不关心它的上下文关系,那么在这样的情况下你应该考虑流机制解析器。


首先,我们来了解一下DOM解析器,也就是树型解析器

我们了解一些可能会用到的一些API

1.DocumentBuilderFactory API

2.DocumentBuilder API

3.Document API

4.Element API

5.Node API

6.CharactorData API

7.NodeList API

8.NamedNodeMap API


以下是解析XML的具体步骤:

1.要读入一个XML文档,首先需要一个DocumentBuilder对象,我们可以从DocumentBuilderFactory中newInstance。

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();


2.读入XML文档,或者是URL,或者是一个任意的输入流InputStream.。Document对象是解析之后的XML文档的树型结构在内存中的表现形式

File f = ...;
Document doc = builder.parse(f);

URL u = ...;
Document doc = builder.parse(u);

InputSteam in = ...;
Document doc = builder.parse(in);


3.得到Document对象doc中的根节点,可以遍历XML文件中的各种对象属性

Element root = doc.getDocumentElement();


4.得到Element对象root的子元素List

NodeList children = root.getChildNodes();

5.对children进行遍历

for(int i = 0 ; i < children.getLength() ; i++){
<span style="white-space:pre">	</span>Node child = children.item(i);<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>...
}<span style="font-family: Arial, Helvetica, sans-serif;">.</span>


附上XML文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<title>
		<fontname>Helvetica</fontname>
		<fontsize>36</fontsize>
	</title>
	<body>
		<fontname>Helvetica</fontname>
		<fontsize>36</fontsize>
	</body>
	<window>

	<heigh>200</heigh>
	<width>400</width>
	
	</window>
	<color>

	<red>0</red>
	<green>50</green>
	<blue>100</blue>
	</color>
	<menu>
		<item>Times Roman </item>
		<item>Helvetica</item>
	</menu>
	
</configuration>


附上DomTest.java
/**  
  
 * @ClassName: DOMtest  
 * @Description: 解析XML文件测试使用(这里用一句话描述这个类的作用)  
 * @author Ricardo Shaw  
 * @email  ricardo_shaw@outlook.com
 * @date 2016年10月13日 下午8:09:56  
  
 *  
    
  
 */
public class DOMtest {
	public static void main(String[] args){
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		System.out.println("DOMtest开始了");
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			InputStream in = new FileInputStream("test.xml");
			Document doc = builder.parse(in);
			Element root = doc.getDocumentElement();
			
			NodeList children = root.getChildNodes();
			for(int i = 0 ; i < children.getLength() ; i++){
				Node child = children.item(i);
				if(child instanceof Element){
				System.out.println("NodeName = "+child.getNodeName());
				NodeList nodes = child.getChildNodes();
					for(int j = 0 ; j < nodes.getLength() ; j++){
						Node  node = nodes.item(j);
						if(node instanceof Element){
							Element element = (Element) node;
							String name = element.getNodeName();
							Text text = (Text)element.getFirstChild();
							String value = text.getData().trim();
							System.out.println("name = "+name+";value = "+value);
						}
					}	
				}
			}
		} catch (ParserConfigurationException | FileNotFoundException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

附上运行结果:

DOMtest开始了
NodeName = title
name = fontname;value = Helvetica
name = fontsize;value = 36
NodeName = body
name = fontname;value = Helvetica
name = fontsize;value = 36
NodeName = window
name = heigh;value = 200
name = width;value = 400
NodeName = color
name = red;value = 0
name = green;value = 50
name = blue;value = 100
NodeName = menu
name = item;value = Times Roman
name = item;value = Helvetica







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值