xml结构和解析

下图为一份xml文件

xml version="1.0" encoding="UTF-8"?>
<struts>
    name="login" class="com.coderising.litestruts.LoginAction">
        name="success">/jsp/homepage.jsp
        name="fail">/jsp/showLogin.jsp
    
    name="logout" class="com.coderising.litestruts.LogoutAction">
       name="success">/jsp/welcome.jsp
       name="error">/jsp/error.jsp
    
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的是

xml version="1.0" encoding="UTF-8"?>
<struts>//root
    name="login" class="com.coderising.litestruts.LoginAction">
 0				1
        name="success">/jsp/homepage.jsp
 0				1
        name="fail">/jsp/showLogin.jsp
 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,即 name="login" class="com.coderising.litestruts.LoginAction">
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,即 name="success">/jsp/homepage.jsp
Node nodeLoginSuccess = resListLogin.item( 1 ) ; String valueLoginSuccess = nodeLoginSuccess.getAttributes().getNamedItem( "name" ).getNodeValue() ; String urlLoginSuccess = nodeLoginSuccess.getTextContent() ; //第二个nodeResult,即 name="fail">/jsp/showLogin.jsp Node loginFail = resListLogin.item( 3 ) ; String valueLoginFail = loginFail.getAttributes().getNamedItem( "name" ).getNodeValue() ; String urlLoginFail = loginFail.getTextContent() ; //第二个nodeAction,即 name="logout" class="com.coderising.litestruts.LogoutAction">
String nameLogout = nodeActionLogout.getAttributes().getNamedItem( "name" ).getNodeValue() ; String classLogou = nodeActionLogout.getAttributes().getNamedItem( "class" ).getNodeValue() ; NodeList resListLogout = nodeActionLogout.getChildNodes() ; //第一个nodeResult,即 name="success">/jsp/welcome.jsp Node nodeLogoutSuccess = resListLogout.item( 1 ) ; String valueLogoutSuccess = nodeLogoutSuccess.getAttributes().getNamedItem( "name" ).getNodeValue() ; String urlLogoutSuccess = nodeLogoutSuccess.getTextContent() ; //第二个nodeResult,即 name="error">/jsp/error.jsp Node nodeLogoutError = resListLogout.item( 3 ) ; String valueLogoutError = nodeLogoutError.getAttributes().getNamedItem( "name" ).getNodeValue() ; String urlLogoutError = nodeLogoutError.getTextContent() ;

你可能感兴趣的:(xml结构和解析)