dom4j解析xml

        导入jar包dom4j-1.6.1.jar。

        XML文件如下所示:

<notice type="jsz">
	<atts name="通知公告">
		<att key="告知编号">
			<dbName>str1</dbName>
		</att>
		<att key="姓名" dbName="str2"></att>
	</atts>
</notice>

        Java代码如下所示:

/**
 * 
 */
package com.gsoft.geloin;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * @author Geloin
 * 
 */
public class MyTest {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {

		String configFile = "c:/Users/Geloin/Desktop/test.xml";

		// 将xml文件读取到Document中。
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(new BufferedReader(
				new InputStreamReader(
						new FileInputStream(new File(configFile)), "GBK")));

		// 获取根节点
		Element root = document.getRootElement();
		// 取根节点的属性
		List<Attribute> rootAtts = root.attributes();
		for (Attribute att : rootAtts) {
			// 打印type===jsz
			System.out.println(att.getName().trim() + "==="
					+ att.getValue().trim());
		}

		// 取下一级子节点
		Iterator<Element> seconds = root.elementIterator();
		while (seconds.hasNext()) {
			Element second = seconds.next();
			// 取第二级的属性
			List<Attribute> secondAtts = second.attributes();
			for (Attribute att : secondAtts) {
				// 打印name===通知公告
				System.out.println(att.getName().trim() + "==="
						+ att.getValue().trim());
			}

			// 取下一级子节点
			Iterator<Element> thirds = second.elementIterator();
			while (thirds.hasNext()) {
				Element third = thirds.next();
				// 取第二级的属性
				List<Attribute> thirdAtts = third.attributes();
				for (Attribute att : thirdAtts) {
					// 打印key===告知编号、key===姓名、dbName===str2
					System.out.println(att.getName().trim() + "==="
							+ att.getValue().trim());
				}

				// 取第四级子节点
				Iterator<Element> fours = third.elementIterator();
				while (fours.hasNext()) {
					Element four = fours.next();
					// 打印dbName===str1
					System.out.println(four.getName().trim() + "===="
							+ four.getTextTrim());
				}
			}
		}
	}
}

        主要方法:

        (1) org.dom4j.io.SAXReader.read(Reader):将XML文件读取到Document里面,注意上述代码中的编码设置,有中文时设置为GBK,若还是乱码,则换成UTF-8;

        (2) org.dom4j.Document.getRootElement():获取根节点;

        (3) org.dom4j.Element.attributes():获取所有的属性;

        (4) org.dom4j.Node.getName():获取属性名或节点名;

        (5) org.dom4j.Attribute.getValue():获取属性值;

        (6) org.dom4j.Element.getTextTrim():获取标签的内容,即<a>text</a>中的text。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值