Document doc = parseXmlFile("infilename.xml", false);
// Add a CDATA section to the root element
Element element = doc.getDocumentElement();

// Special characters are automatically converted to entities by the XML writers
text = doc.createTextNode("<>&\"'");
element.appendChild(text);

// Save the document to the disk file
try
{
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
// aTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF8"); // OutputKeys.ENCODING == "encoding"
aTransformer.setOutputProperty("encoding","UTF-8");
Source src = new DOMSource(doc);
Result dest = new StreamResult(new File("out.xml"));
aTransformer.transform(src, dest);
}
catch (TransformerConfigurationException e)
{
e.printStackTrace();
}
catch (TransformerException e)
{
e.printStackTrace();
}留意// Save the document to the disk file

解决方案 »

  1.   

    难道不能只修改xml文件部分,不要全文覆盖??
      

  2.   

    sorry,来一段完整的(infilename.xml、out.xml都改用infilename.xml)// BasicDom.java
    import java.io.*;
    import javax.xml.parsers.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.*;
    import javax.xml.transform.stream.*; 
    import org.w3c.dom.*;
    import org.xml.sax.*;public class BasicDom {
    public static void main(String[] args) 
    {
    Document doc = parseXmlFile("infilename.xml", false);
    // Add a CDATA section to the root element
    Element element = doc.getDocumentElement();

    // Special characters are automatically converted to entities by the XML writers
    text = doc.createTextNode("<>&\"'");
    element.appendChild(text);

    // Save the document to the disk file
    try
    {
    TransformerFactory tranFactory = TransformerFactory.newInstance();
    Transformer aTransformer = tranFactory.newTransformer();
    // aTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF8"); // OutputKeys.ENCODING == "encoding"
    // aTransformer.setOutputProperty("encoding","UTF-8");
    Source src = new DOMSource(doc);
    Result dest = new StreamResult(new File("infilename.xml"));
    aTransformer.transform(src, dest);
    }
    catch (TransformerConfigurationException e)
    {
    e.printStackTrace();
    }
    catch (TransformerException e)
    {
    e.printStackTrace();
    }
    } // Parses an XML file and returns a DOM document.
    // If validating is true, the contents is validated against the DTD
    // specified in the file.
    public static Document parseXmlFile(String filename, boolean validating)
    {
    try {
    // Create a builder factory
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(validating); // Create the builder and parse the file
    Document doc = factory.newDocumentBuilder().parse(new File(filename));
    return doc;
    } catch (SAXException e) {
    // A parsing error occurred; the xml input is not valid
    } catch (ParserConfigurationException e) {
    } catch (IOException e) {
    }
    return null;
    }
    }
    或者参考
    http://expert.csdn.net/Expert/topic/2243/2243492.xml?temp=.8028833
      

  3.   

    所谓"实际的XML"自然是硬盘上的XML文件,对吧?XML文件只被读了一次,显然不会改变。你改了XML的DOM树,不过是在内存,写回硬盘才会永久保存。就跟我们平时修改文件要存盘是一样的。
      

  4.   

    是不是XML文件的修改就只能是整个文件的重写!