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
// 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
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