本帖最后由 xyflash 于 2009-11-06 21:40:01 编辑

解决方案 »

  1.   

    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 Main {
        public static void main(String[] args) {
            SAXReader saxReader = null;
            Document doc = null;
            try {
                saxReader = new SAXReader();
                doc = saxReader.read(new File("./ss.xml"));
                List<Element> list = doc.selectNodes("//b//c//l//t//i");
                for (Element CdcEntryItem : list) {
                    String key = i.elementTextTrim("Key");
                    if("BankMessage".equals(key)) {
                        System.out.println(i.elementTextTrim("Value"));
                        return;
                    }
                }
            } catch (DocumentException e) {
            }
        }
      

  2.   


    public static void main(String[] args){
    try{
    SAXReader reader = new SAXReader();
    File f = new File(Temp.class.getResource("/temp.xml").getFile());
    Document doc = reader.read(f);
    Element root = doc.getRootElement();
    System.out.println("所有分类:");
    Iterator it = root.elementIterator("l");
    Element foo;
    int num = 0;
    while(it.hasNext()){
    foo = (Element) it.next();
    Iterator _it = foo.elementIterator("t");
    while(_it.hasNext()){
    Element e = (Element)_it.next();
    String tmp = e.attributeValue("name");
    System.out.println("----"+tmp);
    Iterator $it = e.elementIterator("i");
    while($it.hasNext()){
    Element etmp = (Element)$it.next();
    num ++;
    if("分类名称1".equals(tmp)){
    System.out.println("-----------------"+etmp.attributeValue("name"));
    }
    }
    }
    }
    System.out.println("<t>中的子项个数 :------"+num);

    Element element = root.element("l");
    element = element.addElement("t");
    element.addAttribute("name","分类名称3");
    XMLWriter writer = new XMLWriter(new FileWriter(f));  
                writer.write(root);  
                writer.close();   
    }catch(Exception e){
    e.printStackTrace();
    }
    }结果:
    所有分类:
    ----分类名称1
    -----------------子项名称1
    -----------------子项名称2
    ----分类名称2
    <t>中的子项个数 :------4添加分类参考
    http://www.cnblogs.com/kentyshang/archive/2007/06/07/621398.html
      

  3.   

    http://blog.csdn.net/PigHeadSam/archive/2008/10/29/3176230.aspx
    看看这个例子
      

  4.   


    import java.util.List;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.Node;public class TestDom4j {
        public static void main(String[] args) throws Exception{
            String xmlstr = "<?xml version=\"1.0\" encoding=\"GBK\" ?>" 
                            + "<b><c>"
                            + "<name>名称</name>"
                            + "</c><l>"
                            + "<t name=\"分类名称1\"><i name=\"子项名称1\" /><i name=\"子项名称2\" /></t>"
                            + "<t name=\"分类名称2\"><i name=\"子项名称1\" /><i name=\"子项名称2\" /></t>"
                            + "</l></b>";
           //1 
           Document doc = DocumentHelper.parseText(xmlstr);
           List list = doc.selectNodes("//t");
           for(int i = 0; i < list.size(); i++){
               Element e = (Element)list.get(i);
               System.out.println(e.attributeValue("name"));
           }
           //2
           list = doc.selectNodes("//*[@name='分类名称1']/i");
           for(int i = 0; i < list.size(); i++){
               Element e = (Element)list.get(i);
               System.out.println(e.attributeValue("name"));
           }
           //3
           list = doc.selectNodes("count(//t/i)");
           Double d = (Double)list.get(0);
           System.out.println((int)d.doubleValue());
           //4
           Node node = doc.selectSingleNode("//l");
           Element e = (Element)node;
           Element temp = DocumentHelper.createElement("t");
           temp.addAttribute("name", "分类名称3");
           e.add(temp);
           System.out.println(doc.asXML());
        }
    }
      

  5.   

    <t>中的子项个数 是指某个分类中的个数 有没有那个函数可以一次性获取到的 这样循环统计如果有很多个就有点不实惠了  我最早也可虑用while 就没得到分 
    不过 sunnylyy 的这个方法可以试试
     list = doc.selectNodes("//*[@name='分类名称1']/i");
     for(int i = 0; i < list.size(); i++){
      

  6.   

    selectNodes("//*[@name='分类名称1']/i");这括号里的格式又有什么规律??请教 sunnylyy  希望能详细说明下!!!
      

  7.   


    这个是XPath的语法,可以看下面的
    http://www.w3school.com.cn/xpath/xpath_syntax.asp