<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<title>测试dom4j</title>
<script id="1">
<username>zhangsan</username>
<password>124</password>
</script>
</head>
<body>
<result>0</result>
<form>
<banlce id="1">1000</banlce>
<banlce id="2">100</banlce>
<banlce id="3">10</banlce>
<subID>23</subID>
</form>
</body>
</html>用dom4j解析 要注意<banlce id="1">1000</banlce>   <banlce id="2">100</banlce>相同的子节点,给出具体的代码 ,谢啦

解决方案 »

  1.   

    给你个用懂么dom4j读取xml配置文件进行数据库连接的例子吧,如下:一、配置文件:
    <?xml version="1.0" encoding="UTF-8"?>
    <demo>
    <database>
    <driver>com.mysql.jdbc.Driver</driver>
    <url>jdbc:mysql://localhost:3306/test</url>
    <username>root</username>
    <password>admin</password>
    </database>
    </demo>二、编写配置文件对应的javabean-JdbcInf:
    注意:配置文件对应的JavaBean所有的属性名和配置文件的节点名字相同,并且要有相应的get和set方法。
    package com.dom4j.dbconnection;public class JdbcInf {public JdbcInf() {
    // TODO Auto-generated constructor stub
    }private String driverName;private String url;private String username;private String password;//getter() & setter()}三、读取配置文件信息的ConfigReader类:
    package com.dom4j.dbconnection;import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;public class ConfigReader {
    private static ConfigReader instance = new ConfigReader();
    private Document doc = null;
    private JdbcInf jdbcinf = null;//2.私有的构造方法,外部代码不能直接new来实例化它
    private ConfigReader(){
    try {
    //获取文本文档
    doc = new SAXReader().read(Thread.currentThread().
    getContextClassLoader().getResourceAsStream("DBConnection.xml"));
    //采用X-PATH方式获取XML配置文件中的信息
    Element driverEle = (Element)doc.selectObject("/demo/database/driver");
    Element urlEle = (Element)doc.selectObject("/demo/database/url");
    Element usernameEle = (Element)doc.selectObject("/demo/database/username");
    Element passwordEle = (Element)doc.selectObject("/demo/database/password");//把获取得到的信息赋值给JdbcInf实例
    jdbcinf = new JdbcInf();
    jdbcinf.setDriverName(driverEle.getStringValue());
    jdbcinf.setUrl(urlEle.getStringValue());
    jdbcinf.setUsername(usernameEle.getStringValue());
    jdbcinf.setPassword(passwordEle.getStringValue());
    } catch (DocumentException e) {
    e.printStackTrace();
    }
    }//3.公共的静态入口点方法
    public static ConfigReader getInstance(){
    return instance;
    }public JdbcInf getJdbcInf(){
    return jdbcinf;
    }
    }四、获取配置文件信息:
    这里只写核心代码
    JdbcInf ji = ConfigReader.getInstance().getJdbcInf();
    Class.forName(ji.getDriverName());
    conn = DriverManager.getConnection(ji.getUrl(),ji.getUsername(),ji.getPassword());
    System.out.println("=====driver============"+ ji.getDriverName());
    System.out.println("=====url=============="+ ji.getUrl());
    System.out.println("=====name============"+ ji.getUsername());
    System.out.println("=====pwd============="+ ji.getPassword());
    System.out.println("=====conn============="+ conn);基本就这样,LZ散分吧。
      

  2.   

    要banlce这些节点的值,封装到javabean中的,我用elementIterator得不到三个banlce的值
      

  3.   


    package com.xiong.dom4j;import java.io.File;
    import java.util.List;import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;public class Answer100
    {
    @SuppressWarnings("unchecked")
    public static void main(String[] args)
    {
    SAXReader reader = new SAXReader();
    Document doc = null;
    try
    {
    doc = reader.read(new File("D:" + File.separator + "test.xml"));
    }
    catch (Exception ex)
    {
    ex.printStackTrace();
    }
    Element rootElement = doc.getRootElement();
    Element formElement = rootElement.element("body").element("form");
    List<Element> banlceElements = formElement.elements("banlce");
    for (Element element : banlceElements)
    {
    System.out.println("id:" + element.attributeValue("id") + "text:"
    + element.getText());
    } }
    }
      

  4.   


    还想问一下 怎么把得到的三个banlce的值存放到集合中,因为名字是重复的,都是banlce,在map中会覆盖掉
      

  5.   

    看一下dom4j的api文档,你就明白了。