<messages>
   <m1></m1>
   <m2></m2>
</messages><messages> is Element.
<m1> and <m2> are all node.

解决方案 »

  1.   

    //  Get document from the xml file
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(cfgFile);
    doc.normalize();
    //  Get dom from the document
    TransformerFactory tf=TransformerFactory.newInstance();
    Transformer trfm=tf.newTransformer();
    DOMSource dmSrc=new DOMSource(doc);
    StreamResult sr=new StreamResult(new File(cfgFile));
    //  Get the root  element <config>
    Element elmRoot=doc.getDocumentElement();//  Usually,we use class Element not Node to program
    //  Get the element <datasource>
    Element elmDataSource=(Element)elmRoot.getElementsByTagName("data_source").item(0);一般说来,很少直接用Node,因为方法很少。多数情况下造型成Element再使用