xml结构和解析

下图为一份xml文件

<?xml version="1.0" encoding="UTF-8"?>
<struts>
    <action name="login" class="com.coderising.litestruts.LoginAction">
        <result name="success">/jsp/homepage.jsp</result>
        <result name="fail">/jsp/showLogin.jsp</result>
    </action>
    <action name="logout" class="com.coderising.litestruts.LogoutAction">
       <result name="success">/jsp/welcome.jsp</result>
       <result name="error">/jsp/error.jsp</result>
    </action>
</struts>

struts是它的根节点(root)

两个action是structs的子节点(childNodes),result是action的子节点

name="login" class="com.coderising.litestruts.LoginAction"是action节点的两个属性(attributes)

name="success"是result节点的属性,/jsp/homepage.jsp是result的TextContent

需要注意的是,在使用dom解析该文件时,想要从structs节点(root)获取action节点时,action是struts的所有子节点中序列为1的节点,序列为0的是<action前面的空格,如下图所示

<?xml version="1.0" encoding="UTF-8"?>
<struts>//root
    <action name="login" class="com.coderising.litestruts.LoginAction">
 0				1
        <result name="success">/jsp/homepage.jsp</result>
 0				1
        <result name="fail">/jsp/showLogin.jsp</result>
 2				3

即nodeAction = root.getChildNodes().item(1); //item(0)为"   "三个空格

    nodeResult1 = nodeAction.getChildNodes().item(1); //item(0)和item(2)为"       "七个空格

    nodeResult2 = nodeAction.getChildNodes().item(3);


解析案例代码如下:

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.FileInputStream;
import java.io.InputStream;

//获取文件
InputStream in = new FileInputStream("struts.xml");
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(in);
//获取根节点root
Element root = doc.getDocumentElement();
//获取root的所有子节点ChildNodes
NodeList rootList = root.getChildNodes();
//获取Action节点
Node nodeActionLogin = rootList.item(1);//第一个nodeAction
Node nodeActionLogout = rootList.item(3);//第二个nodeAction
//第一个nodeAction,即<action name="login" class="com.coderising.litestruts.LoginAction"></action>
String nameLogin = nodeActionLogin.getAttributes()//获得action节点的属性 .getNamedItem("name")//获得属性中key为name的键值对pair .getNodeValue();//获得name的值String classLogin = nodeActionLogin.getAttributes()//获得action节点的属性 .getNamedItem("class")//获得key为class的键值对pair .getNodeValue();//获得class的值NodeList resListLogin = nodeActionLogin.getChildNodes();//第一个nodeResult,即<result name="success">/jsp/homepage.jsp</result>
Node nodeLoginSuccess = resListLogin.item(1);String valueLoginSuccess = nodeLoginSuccess.getAttributes().getNamedItem("name").getNodeValue();String urlLoginSuccess = nodeLoginSuccess.getTextContent();//第二个nodeResult,即<result name="fail">/jsp/showLogin.jsp</result>Node loginFail = resListLogin.item(3);String valueLoginFail = loginFail.getAttributes().getNamedItem("name").getNodeValue();String urlLoginFail = loginFail.getTextContent();//第二个nodeAction,即<action name="logout" class="com.coderising.litestruts.LogoutAction"></action>
String nameLogout = nodeActionLogout.getAttributes().getNamedItem("name").getNodeValue();String classLogou = nodeActionLogout.getAttributes().getNamedItem("class").getNodeValue();NodeList resListLogout = nodeActionLogout.getChildNodes();//第一个nodeResult,即<result name="success">/jsp/welcome.jsp</result>Node nodeLogoutSuccess = resListLogout.item(1);String valueLogoutSuccess = nodeLogoutSuccess.getAttributes().getNamedItem("name").getNodeValue();String urlLogoutSuccess = nodeLogoutSuccess.getTextContent();//第二个nodeResult,即<result name="error">/jsp/error.jsp</result>Node nodeLogoutError = resListLogout.item(3);String valueLogoutError = nodeLogoutError.getAttributes().getNamedItem("name").getNodeValue();String urlLogoutError = nodeLogoutError.getTextContent();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值