Java 读取xml 文件,精简

package com.skg.utils;java

import java.io.File; import java.util.ArrayList; import java.util.List;web

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

/** *工具

  • @author : oumin
  • @date
  • @desc :2017年2月26日 操做xml 的工具类, 这里主要是解析做用,解析 配置xml配置文件 使用 dom4j
  • 这个读取 bizConfig.xml 文件

*/ public class XmlHelpUtil {.net

/**
 * 
 *  [@author](https://my.oschina.net/arthor) :  oumin
 *  [@date](https://my.oschina.net/u/2504391) :
 *  @desc :2017年2月26日
 *  xmlName 须要读取的 文件名称,这里默认都是  src/main/resources/conf/ 下面的
 *  必须带上 文件的后缀。 
 * @param elements 获取特定名称的子元素 ,
 * 只获取 根元素下面的 子元素。
 * 根节点下面的  对应的 子元素名称。而不是 子元素属性名称
 * @return 返回一个 list,若是是 只要一个话,返回一个 单个 对象的list 。
 * 若是只须要 第一个元素的话, 能够采用 获取list的 第一个对象来使用。
 * @throws Exception 
 * 
 */
public static List<String> readXml(String xmlName ,String elements) throws Exception{
	List<String> textList=new ArrayList<String>();
	
	//文件路径 该文件必须放在 web 里面的resource 里面,不然放入其余模块里面是读取不到的。
	String xmlPath=XmlHelpUtil.class.getClassLoader().getResource("conf/"+xmlName).getFile();		
	
	String xmlPathReal="";
	if (xmlPath.startsWith("file")  ) {
		
		xmlPathReal=xmlPath.substring(6);
	}else {
		
		xmlPathReal= xmlPath;
	}
	
    File xmlfile = new File(xmlPathReal);
  
    
	SAXReader saxReader = new SAXReader();
	// 不进行判断文件是否,存在,不存在就抛异常好了。
	//File xmlfile = new File(xmlPath);
	Document document = saxReader.read(xmlfile);

	// 获取根元素
	Element root = document.getRootElement();
	// 获取特定名称的子元素
	List<Element> childList = root.elements(elements);
	for (Element element : childList) {
		//获取特色名称子元素的 内容,text 
		textList.add(element.getTextTrim());
		
	}
	
	return textList;
	
}

/**
 * 
 *  @author :  oumin
 *  @date :
 *  @desc :2017年2月26日
 *
 * @param args
 * @throws Exception
 * 例子:
 * 
 * 
 * <?xml version="1.0" encoding="UTF-8"?>
<students name="zhangsan">
<hello name="lisi">hello Text1</hello>
<hello name="lisi2">hello Text2</hello>
<hello name="lisi3">hello Text3</hello>
<world name="wangwu">world text1</world>
<world name="wangwu2">world text2</world>
<world >world text3</world>
</students>
 *
 * 
 */
public static void main(String[] args) throws Exception {

	File xmlfile = new File("src/main/resources/conf/bizConfig.xml");
	if (xmlfile.isFile()) {
		System.out.println(">>>找到文件");
	} else {
		System.out.println(">>>没有找到文件");
		return;
	}

	SAXReader saxReader = new SAXReader();
	Document document = saxReader.read(xmlfile);

	// 获取根元素
	Element root = document.getRootElement();
	System.out.println("Root:根元素 " + root.getName());

	// 获取全部子元素
	List<Element> childList = root.elements();
	System.out.println("total child count:全部子元素 数量" + childList.size());

	// 获取特定名称的子元素
	List<Element> childList2 = root.elements("hello");
	
	System.out.println("hello child:都是 hello的子元素数量 " + childList2.size());
	for (Element element : childList2) {
		//System.out.println(">>>节点值"+element.getStringValue());
		System.out.println("hello节点name属性的名称>>"+element.attributeValue("name"));
		System.out.println("hello节点值>>"+element.getTextTrim());
		
	}
	
	

	// 获取名字为指定名称的第一个子元素
	Element firstWorldElement = root.element("world");
	// 输出其属性
	System.out.println("first World Attr: world>> "
			+ firstWorldElement.attribute(0).getName() + "="
			+ firstWorldElement.attributeValue("name")+"##"
			+firstWorldElement.getTextTrim());
	/*
	System.out.println("迭代输出-----------------------");
	// 迭代输出
	for (Iterator iter = root.elementIterator(); iter.hasNext();) {
		Element e = (Element) iter.next();
		System.out.println("节点名称>>"+e.attributeValue("name"));
		System.out.println("节点值>>"+e.getTextTrim());
		

	}*/

}

}code

相关文章
相关标签/搜索