dom4j解析XML

需要解析的XML文件(jtxw.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<DOCUMENT>
	<CHNLDOCS>
		<CHNLDOC name="songjy">
			<ID>5570</ID>
			<TITLE><![CDATA[ 这是集团新闻置顶的文章  ]]></TITLE>
			<DATE><![CDATA[ 2013-10-11]]></DATE>
			<COMPANY><![CDATA[ 信息管理部 ]]></COMPANY>
		</CHNLDOC>
		<CHNLDOC>
			<ID>3036</ID>
			<TITLE text="nnn"><![CDATA[ 一汽大众30万增量来自西南和华南]]></TITLE>
			<DATE><![CDATA[ 2013-10-09  ]]></DATE>
			<COMPANY><![CDATA[ 信息管理部  ]]></COMPANY>
		</CHNLDOC>
	</CHNLDOCS>
	<PAGECOUNT>502</PAGECOUNT>
</DOCUMENT>


package com.songjy.xml;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import junit.framework.TestCase;

public class Dom4j extends TestCase {

	public void xml01() {
		try {
			String xml = FileUtils.readFileToString(new File("D:/jtxw.xml"));
			InputStream in = Dom4j.class.getClassLoader().getResourceAsStream(
					"com/songjy/xml/jtxw.xml");
			SAXReader reader = new SAXReader();

			// Document document = org.dom4j.DocumentHelper.parseText(xml);

			Document document = reader.read(in);

			Element rootElt = document.getRootElement();

			Element element = rootElt.element("CHNLDOCS");

			for (Iterator iterator = element.elementIterator("CHNLDOC"); iterator
					.hasNext();) {
				Element elem = (Element) iterator.next();
				if (null != elem.attribute("name"))
					System.out.println(elem.attribute("name").getText());
				System.out.println("ID::" + elem.elementTextTrim("ID"));
				System.out.println("TITLE::" + elem.elementTextTrim("TITLE"));
				if (null != elem.element("TITLE").attribute("text"))
					System.out.println(elem.element("TITLE").attribute("text")
							.getText());
				System.out.println("DATE::" + elem.elementTextTrim("DATE"));
				System.out.println("COMPANY::"
						+ elem.elementTextTrim("COMPANY"));
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}


dom4j 解析String 数组格式xml

使用dom4j解析xml(转)
使用dom4j 解析 XML
Spring和JAXB2.0结合使用
Xml中SelectSingleNode方法中的xpath用法
Xml中SelectSingleNode方法中的xpath用法

你可能感兴趣的:(xml,解析,dom4j,对象)