java读取xml文件的两种方式

方式一

package accp.dom4j;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class W3cXML {
	
	public static void main(String[] args) {

		//得一文件流
		InputStream is=W3cXML.class.getResourceAsStream("/actions.xml");
		
		//得到一个工厂实例
		DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
		//得到一个dom解析器
		try {
			DocumentBuilder db= dbf.newDocumentBuilder();
			//解析
			try {
				Document doc=db.parse(is);
				//get elemetn
				NodeList actionList=doc.getElementsByTagName("action");
				
				//System.out.println("Dom4jTest.main()"+actionList.getLength());
				for(int i=0;i<actionList.getLength();i++){
					System.out.println("----------------------->");
					Element node=(Element) actionList.item(i);
					String name=node.getAttribute("name");
					System.out.println("acitonName:\t"+name);
					
					String path=node.getElementsByTagName("path").item(0).getFirstChild().getNodeValue().trim();
					String actionClass=node.getElementsByTagName("actionClass").item(0).getFirstChild().getNodeValue().trim();
					
					System.out.println("path:"+path);
					System.out.println("actionClass:"+actionClass);
					
				}
				
				
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		
	}
	
	
}

方式二

package accp.dom4j;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dom4jXML {

	/**
	 * @param args
	 * 不会一次读完,是一个一个的读取
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		InputStream is=Dom4jXML.class.getResourceAsStream("/actions.xml");
		//解
		Document doc;
		try {
			doc = new SAXReader().read(is);
			//root
			Element root=doc.getRootElement();
			
			//reader
			List<Element> actionList=root.elements("action");
			
			//读取值
			for(Element el:actionList){
				System.out.println("*****************");
				System.out.println("Action--name:\t"+el.attributeValue("name"));								
				System.out.println("path:"+el.element("path").getTextTrim());
				System.out.println("class:"+el.element("actionClass").getTextTrim());	
					
			}
			
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}

}

声明一下,我取的类名字搞反了!哈哈,dom4j和w3c

对应的XML文件格式

<?xml version="1.0" encoding="UTF-8"?>
<actions>

<action name="login" >
<path>/Login</path>
<actionClass>accp.service.Login</actionClass>
</action>

<action name="register">
<path>/Login</path>
<actionClass>accp.service.Login</actionClass>
</action>


</actions>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值