The example in this article describes the Java operations XML method. Share to everyone for your reference. Specifically as follows:
The Java code is as follows:
Copy Code code as follows:
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import org.w3c.dom.*;
Import org.xml.sax.SAXException;
Import javax.xml.parsers.*;
Import javax.xml.transform.*;
Import Javax.xml.transform.dom.DOMSource;
Import javax.xml.transform.stream.*;
Import javax.xml.xpath.*;
public class Test {
public static void Main (string[] args) {
Documentbuilderfactory factory=documentbuilderfactory.newinstance ();
Element Thebook=null, Theelem=null, Root=null;
try {
Factory.setignoringelementcontentwhitespace (TRUE);
Documentbuilder Db=factory.newdocumentbuilder ();
Document Xmldoc=db.parse (New File ("Test1.xml"));
Root=xmldoc.getdocumentelement ();
thebook= (Element) selectSingleNode ("/books/book[name= ' Harry Potter ']", root);
SYSTEM.OUT.PRINTLN ("---Search for Harry Potter"----);
Element namenode= (Element) thebook.getelementsbytagname ("Price"). Item (0);
String name=namenode.getfirstchild (). Getnodevalue ();
SYSTEM.OUT.PRINTLN (name);
Output (Thebook);
System.out.println ("=============selectsinglenode (books/book[name= ' Harry Potter '], root) ==================");
---Start a new book----
Thebook=xmldoc.createelement ("book");
Theelem=xmldoc.createelement ("name");
Theelem.settextcontent ("new book");
Thebook.appendchild (Theelem);
Theelem=xmldoc.createelement ("Price");
Theelem.settextcontent ("20");
Thebook.appendchild (Theelem);
Theelem=xmldoc.createelement ("Memo");
Theelem.settextcontent ("The new book looks better.") ");
Thebook.appendchild (Theelem);
Root.appendchild (Thebook);
SYSTEM.OUT.PRINTLN ("---a new book begins----");
Output (xmldoc);
System.out.println ("==============================");
---A new book completed----
---Make some changes to "Harry Potter" below. ----
---Search for Harry Potter----
---Revise the price of this book at this time-----
Thebook.getelementsbytagname ("Price"). Item (0). Settextcontent ("a");//getelementsbytagname returns NodeList, So keep up with item (0). In addition, getElementsByTagName ("price") corresponds to the XPath ".//price".
SYSTEM.OUT.PRINTLN ("---Revise the price of the book at this time----");
Output (Thebook);
---also want to add a property ID, the value is B01----
Thebook.setattribute ("id", "B01");
SYSTEM.OUT.PRINTLN ("---also want to add a property ID, the value is B01----");
Output (Thebook);
---to the "Harry Potter" to complete the revision. ----
---Want to delete the book "The Two Kingdoms" with the id attribute----
thebook= (Element) selectSingleNode ("/books/book[@id = ' B02 ']", root);
SYSTEM.OUT.PRINTLN ("---to delete" The novel of the Kingdoms of the----"with id attribute);
Output (Thebook);
Thebook.getparentnode (). removechild (Thebook);
SYSTEM.OUT.PRINTLN ("---deleted XML----");
Output (xmldoc);
---to delete all books that are below 10----
NodeList somebooks=selectnodes ("/books/book[price<10]", root);
SYSTEM.OUT.PRINTLN ("---to delete all books below the price of 10---");
SYSTEM.OUT.PRINTLN ("---Qualified book has" +somebooks.getlength () + "Ben. ---");
for (int i=0;i<somebooks.getlength (); i++) {
Somebooks.item (i). Getparentnode (). RemoveChild (Somebooks.item (i));
}
Output (xmldoc);
SaveXML ("Test1_edited.xml", xmldoc);
catch (Parserconfigurationexception e) {
E.printstacktrace ();
catch (Saxexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}
public static void output (node node) {//Output node XML string to the console
Transformerfactory transfactory=transformerfactory.newinstance ();
try {
Transformer Transformer = Transfactory.newtransformer ();
Transformer.setoutputproperty ("Encoding", "gb2312");
Transformer.setoutputproperty ("Indent", "yes");
Domsource source=new Domsource ();
Source.setnode (node);
Streamresult result=new Streamresult ();
Result.setoutputstream (System.out);
Transformer.transform (source, result);
catch (Transformerconfigurationexception e) {
E.printstacktrace ();
catch (Transformerexception e) {
E.printstacktrace ();
}
}
public static node selectSingleNode (String Express, Object Source) {//Lookup node, and returns the first qualifying node
Node Result=null;
Xpathfactory xpathfactory=xpathfactory.newinstance ();
XPath Xpath=xpathfactory.newxpath ();
try {
result= (Node) xpath.evaluate (Express, Source, Xpathconstants.node);
catch (Xpathexpressionexception e) {
E.printstacktrace ();
}
return result;
}
The public static nodelist selectnodes (String Express, Object Source) {//Lookup node, returns the set of nodes that meet the criteria.
NodeList Result=null;
Xpathfactory xpathfactory=xpathfactory.newinstance ();
XPath Xpath=xpathfactory.newxpath ();
try {
result= (nodelist) xpath.evaluate (Express, Source, Xpathconstants.nodeset);
catch (Xpathexpressionexception e) {
E.printstacktrace ();
}
return result;
}
public static void SaveXML (String fileName, Document doc) {//Output document to file
Transformerfactory transfactory=transformerfactory.newinstance ();
try {
Transformer Transformer = Transfactory.newtransformer ();
Transformer.setoutputproperty ("Indent", "yes");
Domsource source=new Domsource ();
Source.setnode (DOC);
Streamresult result=new Streamresult ();
Result.setoutputstream (FileName) (new FileOutputStream);
Transformer.transform (source, result);
catch (Transformerconfigurationexception e) {
E.printstacktrace ();
catch (Transformerexception e) {
E.printstacktrace ();
catch (FileNotFoundException e) {
E.printstacktrace ();
}
}
}
The XML file is as follows:
Copy Code code as follows:
<?xml version= "1.0" encoding= "GBK"?>
<books>
<book>
<name> Harry Potter </name>
<price>10</price>
<memo> This is a very nice book. </memo>
</book>
<book id= "B02" >
<name> Kingdoms </name>
<price>10</price>
One of the four famous <memo>. </memo>
</book>
<book id= "B03" >
<name> margin </name>
<price>6</price>
One of the four famous <memo>. </memo>
</book>
<book id= "B04" >
<name> Red Mansion </name>
<price>5</price>
One of the four famous <memo>. </memo>
</book>
</books>
I hope this article will help you with your Java programming.