Element root = doc.getRootElement(); //得到根元素        java.util.List books = root.getChildren(); //得到根元素所有子元素的集合        Element book = (Element)books.get(0); //得到第一个book元素        //为第一本书添加一条属性        Attribute a = new Attribute("hot","true");          book.setAttribute(a);        Element author = book.getChild("author"); //得到指定的字元素        author.setText("王五"); //将作者改为王五        //或 Text t = new Text("王五");book.addContent(t);        Element price = book.getChild("price"); //得到指定的字元素        //修改价格,比较郁闷的是我们必须自己转换数据类型,而这正是JAXB的优势        author.setText(Float.toString(50.0f));                   String indent = "    ";        boolean newLines = true;        XMLOutputter outp = new XMLOutputter(indent,newLines,"GBK");        outp.output(doc, new FileOutputStream("exampleB.xml"));

解决方案 »

  1.   

    不好意思,好像org.w3c.dom 这个包中的document对象没有 getRootElement()这个method啊,你这个doc 是怎么定义的?
      

  2.   

    final String url = "e:\\xht\\download\\00TH000200303261.xml"; 
    com.ibm.xml.parsers.DOMParser parser = new com.ibm.xml.parsers.DOMParser();
    parser.parse(url);
    org.w3c.dom.Document dom = parser.getDocument();
                   org.w3c.dom.NodeList nodelist = dom.getElementsByTagName("email");
                   org.w3c.dom.Node node = nodelist.item(0);
    out.println(node.getNodeValue());现在改成这样,能取到nodelist.getLength() 为 1
    但node.getNodeValue()总是为null
      

  3.   

    好了,终于自己弄清楚了,还要对node取childenode,用childnode.getnodevalue()才能取到值
      

  4.   

    com.ibm.xml.parsers.DOMParser parser = new com.ibm.xml.parsers.DOMParser();
    parser.parse(url);
    org.w3c.dom.Document dom = parser.getDocument();
    org.w3c.dom.NodeList nodelist = dom.getElementsByTagName("email");
    org.w3c.dom.Node node = nodelist.item(0);
    org.w3c.dom.Node childnode = node.getFirstChild();
      
    if (dom!=null)
    {
    out.println(childnode.getNodeValue());
    }else {
    out.println("If you see this message, it is not working");
    }必须取得textnode 才能取得值,一开始你取到的nodetype为elementnode