dom4j解析xml

需要引入dom4j的jar包

struts.xml(工程src目录下)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 	<package name="user" extends="struts-default" namespace="/user">
		<action name="*User" class="com.test.user.action.UserAction" method="{1}User">
		  	<result name="success" type="chain">
		  		<param name="actionName">queryUsers</param>
		  		<param name="namespace">/user</param>
		    </result>
		    <result name="error" >/error.jsp</result>
		</action> 
	 	<action name="queryUsers" class="com.test.user.action.UserAction" method="queryUser">
	  		<result name="users">/show.jsp</result>
	 	</action>
 	</package>
</struts>]

 

解析xml

 

import java.util.List;

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


public class Dom4jReaderXML {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws DocumentException {
		SAXReader saxReader = new SAXReader();
		//1.读取xml文件
		Document doc = saxReader.read(Dom4jReaderXML.class.getClassLoader().getResourceAsStream("struts.xml"));
		//还可以通过下面方法读取
		//Document doc = saxReader.read("src/struts.xml");
		//以下两种方式是通过绝对路径读取xml文件
		//String path = "D:/MyEclipse 8.6/spring/springmvc/src/struts.xml";
		//Document doc = saxReader.read(path);
		//Document doc = saxReader.read(new File(path));
		Element root = doc.getRootElement();//获取根元素struts
		Element element = root.element("package");//获取元素package
		//System.out.println(element.asXML());//获取package节点的内容
		System.out.println("package name=" + element.attributeValue("name"));
		//遍历元素package下所有的action元素
		for(Element action_element : (List<Element>)element.elements("action")) {
			//获取元素action的属性
			System.out.print("  action name=" + action_element.attributeValue("name"));
			System.out.print(" class=" + action_element.attributeValue("class"));
			System.out.println(" method=" + action_element.attributeValue("method"));
			//遍历元素action下所有的result元素
			for(Element rusult_element : (List<Element>)action_element.elements("result")) {
				//获取元素result的属性
				System.out.print("    result name=" + rusult_element.attributeValue("name"));
				if(rusult_element.isTextOnly()) {
					System.out.println(" value=" + rusult_element.getText());
				} else {
					System.out.println();
					//遍历元素result下所有的param元素
					for(Element param_elment : (List<Element>)rusult_element.elements("param")) {
						//获取元素param的属性
						System.out.print("      param name=" + param_elment.attributeValue("name"));
						System.out.println(" value=" + param_elment.getText());
					}
				}
			}
		}
	}
}

  

运行结果

package name=user
  action name=*User class=com.test.user.action.UserAction method={1}User
    result name=success
      param name=actionName value=queryUsers
      param name=namespace value=/user
    result name=error value=/error.jsp
  action name=queryUsers class=com.test.user.action.UserAction method=queryUser
    result name=users value=/show.jsp

 

你可能感兴趣的:(xml,dom4j)