Java reads and writes XML files

Source: Internet
Author: User
Package editXML; import java. io. file; import java. io. fileOutputStream; import java. io. printWriter; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import javax. xml. transform. outputKeys; import javax. xml. transform. transformer; import javax. xml. transform. transformerFactory; import javax. xml. transform. dom. DOMSource; import javax. xml. transform. stream. streamResult; import org. w3c. dom. document; import org. w3c. dom. element; import org. w3c. dom. node; import org. w3c. dom. nodeList; public class w3c_Xml {static DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); static DocumentBuilder builder = null; public static void readXML () {try {builder = factory. newDocumentBuilder (); Document document = builder. parse (new File ("E: \ testFiles \ test. xml "); Element rootElement = document. getDocumentElement (); NodeList list = rootElement. getElementsByTagName ("Header"); Element element = (Element) list. item (0); System. out. println (element. getChildNodes (). item (0 ). getNodeValue ();} catch (Exception e) {System. out. println ("exception:" + e. getMessage () ;}} public static void writeXML (Document document, String filename) {try {builder = factory. newDocumentBuilder (); // Document document = builder. parse (new File ("E: \ testFiles \ test. xml "); document. normalize ();/** write the content in the document to the file */TransformerFactory tFactory = TransformerFactory. newInstance (); Transformer transformer = tFactory. newTransformer (); transformer. setOutputProperty (OutputKeys. ENCODING, "UTF-8"); transformer. setOutputProperty (OutputKeys. INDENT, "yes"); // code DOMSource source = new DOMSource (document); PrintWriter pw = new PrintWriter (new FileOutputStream (filename )); streamResult result = new StreamResult (pw); transformer. transform (source, result);} catch (Exception e) {e. printStackTrace () ;}} public static void updateXML () {try {builder = factory. newDocumentBuilder (); Document document = builder. parse (new File ("E: \ testFiles \ test. xml "); Node root = document. getDocumentElement ();/** if root has sub-elements */if (root. hasChildNodes () {NodeList ftpnodes = root. getChildNodes ();/** get all ftpnodes nodes cyclically */for (int I = 0; I <ftpnodes. getLength (); I ++) {Node ftpList = ftpnodes. item (I); System. out. println (ftpList. getTextContent () ;}for (int I = 0; I <ftpnodes. getLength (); I ++) {Node ftpList = ftpnodes. item (I); ftpList. setTextContent (ftpList. getTextContent () + "update") ;}} writeXML (document, "E: \ testFiles \ test2.xml");} catch (Exception e) {e. printStackTrace () ;}} public static void main (String [] args) {// read readXML (); // write // writeXML (document, "E: \ testFiles \ test. xml "); // update updateXML ();}}

Test. xml

 

 

 <?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>

<body>Don't forget the meeting!</body>
</note>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.