日期:2014-05-20  浏览次数:20664 次

如何获取这个xml标签里的值啊?
有工程1. grpIndustryWeb_1 Location为E:\program\grpSmartEnterpriseApp\WebContent\WEB-INF\web.xml
  2. z_mytest Location为E:\program\z_mytest\src\Test1.java


/grpSmartEnterpriseApp/WebContent/WEB-INF/web.xml


我想在/z_mytest/src/Test1.java里面写代码,
其功能是读取/grpSmartEnterpriseApp/WebContent/WEB-INF/web.xml里面的两个标签。

请教怎么写啊?

Java code
public class Test1 {
    public static void main(String args[]) {
        //这里先找到文件web.xml
    /*
    读下面这个标签:
        <display-name>grpSmartEnterpriseApp</display-name>
    */
    String displayName=//这里获取上面标签里的值:grpSmartEnterpriseApp
    /*
    读下面这个标签:
    <servlet-mapping>
        <servlet-name>Schema_1Service</servlet-name>
        <url-pattern>/servlet/schema_1/Service</url-pattern>
    </servlet-mapping>
    */
    String urlPattern=//这里获取上面标签里的值:/servlet/schema_1/Service
    
    String myurl="http://localhost:8080/";
    myurl=myrul+servletName+urlPattern;
    System.out.println(myurl);
    
    }
}

/grpSmartEnterpriseApp/WebContent/WEB-INF/web.xml内容如下:

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

XML code
<web-app id="web-app_1" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
    <display-name>grpSmartEnterpriseApp</display-name>
    <distributable />
    
    <listener>
        <listener-class>grp.servlet.GrpSessionListener</listener-class>
    </listener>

    <servlet>
        <display-name>Schema_1CatalogueServlet</display-name>
        <servlet-name>Schema_1CatalogueServlet</servlet-name>
        <servlet-class>grp.industry.schema_1.servlets.CatalogueServlet</servlet-class>

    </servlet>
    <!--省略部分标签-->
        <servlet-mapping>
        <servlet-name>Schema_1Service</servlet-name>
        <url-pattern>/servlet/schema_1/Service</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/servlet/security/Login</url-pattern>
    </servlet-mapping>
</web-app>


------解决方案--------------------
Java code

        try
        {
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File("F://web.xml")); //这里记着把文件路径改了
            
            Element root = document.getRootElement();
            Element dnE = root.element("display-name");
            String displayName = dnE.getText();
            
            Element smE = root.element("servlet-mapping");
            Element upE = smE.element("url-pattern");
            String urlPattern = upE.getText();
            
            System.out.println(displayName);
            System.out.println(urlPattern);
            
        }
        catch(DocumentException e)
        {
            e.printStackTrace();
        }

------解决方案--------------------
HTML code
<?xml version="1.0" encoding="UTF-8" ?>
<list>
 <serverGroup name="default"> 
  <server hostname ="192.168.0.0" ip="192.168.0.0" port="1024"/> 
 </serverGroup> 
 <serverGroup name="servergroup1"> 
  <server hostname ="192.168.0.1" ip="192.168.0.1" port="1024"/> 
  <server hostname ="192.168.0.2" ip="192.168.0.2" port="1024"/> 
  <server hostname ="192.168.0.3" ip="192.168.0.3" port="1024"/> 
 </serverGroup> 
 <serverGroup name="servergroup1"> 
  <server hostname ="192.168.0.4" ip="192.168.0.4" port="1024"/> 
  <server hostname ="192.168.0.5" ip="192.168.0.5" port="1024"/>  
 </serverGroup>
</list>