dom4j自己的jar包里面是不包含xpath处理的功能的,你想用xpath的功能,需要一个专门的xpath处理包poi-2.5.1-final-20040804.jar(版本可能不同,名字应该差不多),在你下载的dom4j里面找找,应该能找到的

解决方案 »

  1.   

    public static Document getDocument(String pFileName) {
        if(pFileName==null||pFileName.equals("")) return null;
        return getDocument(new File(pFileName));
      }  public static Document getDocument(File pFile) {
        if(pFile==null || !pFile.exists()) return null;
        try {
          DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
          if(dbf==null) return null;
          DocumentBuilder df=dbf.newDocumentBuilder();
          if(df==null) return null;
          return df.parse(pFile);
        }
        catch (Exception any) {
          logger.error(any);
          return null;
        }
      }
        try{
          Document doc=getDocument("the file.xml");
          String xpath="//main/books";      Node node =  XPathAPI.selectSingleNode(doc, xpath);
          if(node!=null){
              Element _node=doc.create.....Element("book");
              _node......set //or add Text
              node.add....Element or Node( _node);
          }
          //to save the document;
      }catch(Exception any){
          logger.error(any);
        }
      

  2.   

    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NamedNodeMap;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.Attr;
    import org.w3c.dom.Node;
    import org.apache.xpath.XPathAPI;
      

  3.   

    我测试了一下,程序没有任何问题.
    运行后test.xml的内容为
    <?xml version="1.0" encoding="GBK"?><main>
      <books>
        <book>mybook</book>
      </books>
    </main>你看一下是不是你DOM4j的版本问题,我用的是1.4的.