zoukankan      html  css  js  c++  java
  • java载入XML文件并解析xml

    import java.io.File;
    import java.util.List;

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

    public class Test {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    SAXReader saxReader = new SAXReader();
    try {
    Document document = saxReader.read(new File("d://demo.xml"));
    Element root = document.getRootElement();
    List actions = root.elements("action");
    for (int i = 0; i < actions.size(); i++) {
    Element action = (Element) actions.get(i);
    System.out.println(" " + "action.path--->" + action.attributeValue("path"));
    System.out.println("action.class--->" + action.attributeValue("class"));

    List forwards = action.elements("forward");
    for(int j=0;j<forwards.size();j++){
    Element forward = (Element) forwards.get(j);
    System.out.println("forward.name--->" + forward.attributeValue("name"));
    System.out.println("forward.url--->" + forward.attributeValue("url"));
    }
    }
    } catch (DocumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }


    <!--xml文件-->

    <?

    xml version="1.0" encoding="UTF-8"?> 
    <actions>
    <action path="/chen" class="com.demo.testCHEN">
    <forward name="success" url="xiao-chen"/>
    <forward name="fail" url="da-chen"/>
    </action>
    <action path="/bei" class="com.demo.testBEI">
    <forward name="success" url="xiao-bei"/>
    <forward name="fail" url="da-bei"/>
    </action>
    </actions>


    执行结果:

    action.path--->/chen
    action.class--->com.demo.testCHEN
    forward.name--->success
    forward.url--->xiao-chen
    forward.name--->fail
    forward.url--->da-chen


    action.path--->/bei
    action.class--->com.demo.testBEI
    forward.name--->success
    forward.url--->xiao-bei
    forward.name--->fail
    forward.url--->da-bei

  • 相关阅读:
    Python元组、列表、字典
    测试通过Word直接发布博文
    Python环境搭建(windows)
    hdu 4003 Find Metal Mineral 树形DP
    poj 1986 Distance Queries LCA
    poj 1470 Closest Common Ancestors LCA
    poj 1330 Nearest Common Ancestors LCA
    hdu 3046 Pleasant sheep and big big wolf 最小割
    poj 3281 Dining 最大流
    zoj 2760 How Many Shortest Path 最大流
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5139179.html
Copyright © 2011-2022 走看看