用xpath实现
需要用到dom4j.jar和jaxen.jar        StringBuilder builder = new StringBuilder();
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new File(""));
        Element root = document.getRootElement();
        List<Element> list = root.selectNodes("//resource");
        for (Element e : list) {
            builder.append(e.attributeValue("name")).append(",");
        }

解决方案 »

  1.   

    Document doc = sax.build("test.xml");  
                Element rootEle = doc.getRootElement();                          
                List resourcesList = XPath.selectNodes(rootEle, "//resources/resource");  
    // 循环list
      

  2.   

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("src/test.xml");
    XPathFactory xFactory = XPathFactory.newInstance();
    XPath xpath = xFactory.newXPath();
    XPathExpression expr = xpath.compile("//resource/@name");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
    System.out.println(nodes.item(i).getTextContent());
    }xpath
      

  3.   


    不知道导入哪个类,能不能把 import 部分也贴出来啊
      

  4.   

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpression;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
      

  5.   


    运行成功 但是好像结果是空的
    builder就是你要的值,可以用builder.toString()转换为字符串。