Simple example of java implementing XML add element operations


This article is an example of how java implements XML adding elements. To share for your reference, specific as follows:

package Day01;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
public class CRUDDEMO {
  /*public void addElement() throws Exception{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File ("src/Day01/Book.xml"));
    Element newEle = doc.createElement(" The author ");
    newEle.setTextContent("ZC");
    Node nod = doc.getElementsByTagName(" book ").item(0);
    nod.appendChild(newEle);
    Source sour = new DOMSource(doc);
    Result result = new StreamResult (new FileOutputStream("src/Day01/Book.xml"));
    write (sour, result);
  }*/
  public void addElement2() throws Exception{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // Set up the factory
    DocumentBuilder builder = factory.newDocumentBuilder(); // get builder
    Document doc = builder.parse(new File ("src/Day01/Book.xml")); // To obtain document This is the ultimate goal
    Element newEle = doc.createElement(" The author ");//  Create a new element / The label
    newEle.setTextContent("ZC"); // Set the content to the element  < The author >ZC</ The author >
    Node nod = doc.getElementsByTagName(" Title: ").item(0); // through nodelist the item() Method to obtain a specific node
    /**
     *  Use to insert elements at specific nodes   node .insertBefore methods
     *  The first 1 The parameter is the new node to be inserted 2 One is the insertion position
     */
    nod.insertBefore(newEle, doc.getElementsByTagName(" Title: ").item(0));
    /**
     * DOMSource(Node n)
     *  Pay attention to  element is Node the 1 Subclasses, so you can put doc Put in the constructor
     *
     *
     */
    Source sour = new DOMSource(doc);
    Result result = new StreamResult (new FileOutputStream("src/Day01/Book.xml"));
    write (sour, result);
  }
  public void write(Source source,Result result) {
    TransformerFactory tffactory = TransformerFactory.newInstance();
    Transformer tr;
    try {
      tr = tffactory.newTransformer();
      tr.transform(source, result);
    } catch (TransformerConfigurationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (TransformerException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  public static void main(String[] args) throws Exception {
    CRUDDEMO cr = new CRUDDEMO();
    cr.addElement2();
  }
}

XML before modification:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
< A bookcase >
  < book >
    < Title: >Thinking in Java</ Title: >
    < The author >Eric</ The author >
    < price >$34</ price >
  </ book >
</ A bookcase >

Revised XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
< A bookcase >
  < book >
    < The author >ZC</ The author >
    < Title: >Thinking in Java</ Title: >
    < The author >Eric</ The author >
    < price >$34</ price >
  </ book >
</ A bookcase >

PS: Here are some more online tools for xml:

Online XML/JSON interconversion tool: http://tools.ofstack.com/code/xmljson

XML online format/XML online compression: http://tools.ofstack.com/code/xmlformat

XML Online compression/formatting tool: http://tools.ofstack.com/code/xml\_format\_compress

XML code online formatting tool: http://tools.ofstack.com/code/xmlcodeformat

I hope this article has been helpful in java programming.