Java reads with DOM, updates XML file

Source: Internet
Author: User

package com.sun.xml;import java.io.file;import java.io.filenotfoundexception;import  java.io.fileoutputstream;import java.io.ioexception;import java.io.outputstreamwriter;import  java.io.unsupportedencodingexception;import javax.xml.parsers.documentbuilder;import  Javax.xml.parsers.documentbuilderfactory;import javax.xml.parsers.parserconfigurationexception;import  javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import  javax.xml.transform.transformerexception;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;import org.w3c.dom.text;import org.xml.sax.saxexception;public class  Readxml {    public static void main (STRING[]&NBSp;args)  throws ParserConfigurationException, TransformerException {         document document=read ("C:\\users\\administrator\\desktop\\bookstore1.xml") ;         string path= "c:\\users\\administrator\\desktop\\ Bookstore1.xml ";         addnode (document, path, " Zhang San ");     }    public static document  read (String path)  throws ParserConfigurationException, TransformerException{         document document=null;        documentbuilderfactory  bf=documentbuilderfactory.newinstance ();         documentbuilder  builder=bf.newdocumentbuilder ();        file file=new  File (path);         try {             document= builder.parse (file);             //gets the tag with the title, and returns the collection              Nodelist nodelist=document.getelementsbytagname ("title");                   for (Int i=0;i<nodelist.getlength (); i++) {                 node node =nodelist.item (i);                 string string=node.gettextcontent ();                 system.out.println (String);                  updatexmlnode (document, node,  "Sssss",  path);             }                     } catch  (saxexception | ioexception  e)  {            // TODO  auto-generated catch block             E.printstacktrace ();        }         return document;    }    public static void  updatexmlnode (Document document,node node,string text,string path)  throws  transformerexception, unsupportedencodingexception, filenotfoundexception{         nodE.settextcontent (text);         writetofile (Document, path);     }        public static void addnode ( Document document,string path,string text) {        text  text2=document.createtextnode (text);         text text3= document.createTextNode (text);         text text4= document.createTextNode (text);                 element element=document.createelement ("book");         element element2=document.createelement ("title");         element element3=document.createelement ("author");             element eleMent4=document.createelement ("Price");                 element2.appendchild (TEXT2);         Element3.appendchild (TEXT3);         element4.appendchild (TEXT4);         element.appendchild (Element2);         element.appendchild (ELEMENT3);         element.appendchild ( ELEMENT4);        nodelist nodelist=  document.getElementsByTagName ("bookstore");         node  Node=nodelist.item (0);         node.appendchild (Element);         try {             writetofile (Document, path);         } catch  (unsupportedencodingexception |  filenotfoundexception                 | transformerexception e)  {             // TODO Auto-generated catch block             e.printstacktrace ();         }     }        public static void  WriteToFile (Document document,string path)  throws TransformerException,  unsupportedencodingexception, filenotfoundexception{         Transformerfactory transformerfactory=transformerfactory.newinstance ();         transformer transformer=transformerfaCtory.newtransformer ();        /*          *  format output XML document, line wrap and indent */           Transformer.setoutputproperty (outputkeys.indent,  "yes");           transformer.setoutputproperty ("{http://xml.apache.org/xslt}indent-amount",  "4");         //transformer.transform (New domsource (document), new  Streamresult (path));        /*          *  set the output stream encoding to Utf-8*/        outputstreamwriter  owriter=new outputstreamwriter (New fileoutputstream (new file (path)), "UTF-8");         document.setxmlstandalone (True);         transformer.transform (NEW&NBsp;domsource (document), New streamresult (Owriter));     }} 


The XML file is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <bookstore> <!--record book information--<book> <title> data Structure & lt;/title> <author> Min </author> <price>30.00</price> </book></bookstore>


This article is from the "matengbing" blog, make sure to keep this source http://matengbing.blog.51cto.com/11395502/1876353

Java reads with DOM, updates XML file

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.