1, Java code:
The main use of DOM to operate
Copy Code code as follows:
Package test;
Import java.io.IOException;
Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import javax.xml.parsers.ParserConfigurationException;
Import Javax.xml.transform.Transformer;
Import javax.xml.transform.TransformerConfigurationException;
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.NodeList;
Import Org.w3c.dom.Text;
Import org.xml.sax.SAXException;
public class Xmloprate {
Document Doc;
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
Documentbuilder Builder;
NodeList Imags;
String path;
Public NodeList Getimags () {
return imags;
}
public void Setimags (NodeList imags) {
This.imags = Imags;
}
/**
* Construction Method
* @param the path to the Path:xml file
* @param nodes: XML node name to resolve
*/
Public xmloprate (String path) {
Super ();
This.path = path;
System.out.println (System.getproperty ("User.dir"));
}
/**
* Parsing xml
* @param path
*/
public void readXml () {
try {
Builder = Factory.newdocumentbuilder ();
Document doc=builder.parse (path);
Doc.normalize ();
NodeList imags =doc.getelementsbytagname ("Imags");
This.setimags (imags);
for (int i=0;i<imags.getlength (); i++) {
Element link= (Element) Imags.item (i);
System.out.print ("title:");
System.out.println (Link.getelementsbytagname ("title"). Item (0). Getfirstchild (). Getnodevalue ());
System.out.print ("URL:");
System.out.println (link.getelementsbytagname ("url"). Item (0). Getfirstchild (). Getnodevalue ());
System.out.print ("IMGSRC:");
System.out.println (Link.getelementsbytagname ("Imgsrc"). Item (0). Getfirstchild (). Getnodevalue ());
System.out.println ();
}
}catch (Parserconfigurationexception e) {
E.printstacktrace ();
catch (Saxexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}
/**
* Addcode
* @param path
*/
public void Addxmlcode (String imgsrc,string title,string URL) {
try {
Builder = Factory.newdocumentbuilder ();
Document doc=builder.parse (path);
Doc.normalize ();
Text textseg;
Element imag=doc.createelement ("Imags");
Element linkimgsrc=doc.createelement ("imgsrc");
Textseg=doc.createtextnode (IMGSRC);
Linkimgsrc.appendchild (TEXTSEG);
Imag.appendchild (LINKIMGSRC);
Element linktitle=doc.createelement ("title");
Textseg=doc.createtextnode (title);
Linktitle.appendchild (TEXTSEG);
Imag.appendchild (LinkTitle);
Element linkurl=doc.createelement ("url");
Textseg=doc.createtextnode (URL);
Linkurl.appendchild (TEXTSEG);
Imag.appendchild (Linkurl);
Doc.getdocumentelement (). appendchild (IMAG);
Transformerfactory tfactory =transformerfactory.newinstance ();
Transformer Transformer;
Transformer = Tfactory.newtransformer ();
Domsource Source = new Domsource (DOC);
Streamresult result = new Streamresult (new Java.io.File (path));
Transformer.transform (source, result);
}catch (Exception e) {
}
}
/**
* Delete XML code
* @param path
*/
public void Delxmlcode () {
try {
Builder = Factory.newdocumentbuilder ();
Doc=builder.parse (path);
Doc.normalize ();
NodeList imags =doc.getelementsbytagname ("Imags");
Element elink= (Element) Imags.item (0);
Elink.removechild (Elink.getelementsbytagname ("Imgsrc"). Item (0));
Elink.removechild (Elink.getelementsbytagname ("title"). Item (0));
Elink.removechild (elink.getelementsbytagname ("url"). Item (0));
Doc.getfirstchild (). removechild (Elink);
Transformerfactory tfactory =transformerfactory.newinstance ();
Transformer Transformer = Tfactory.newtransformer ();
Domsource Source = new Domsource (DOC);
Streamresult result = new Streamresult (new Java.io.File (path));
Transformer.transform (source, result);
catch (Parserconfigurationexception e) {
E.printstacktrace ();
catch (Saxexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
catch (Transformerconfigurationexception e) {
E.printstacktrace ();
catch (Transformerexception e) {
E.printstacktrace ();
}
}
}
2, the XML part
Copy Code code as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<root>
<imags>
images/ad-01.jpg</imgsrc>
<title> Ho Chi Minh City </title>
<url>http://torchrelay.beijing2008.cn/cn/journey/hochiminhcity/</url>
</imags>
<imags>
images/ad-02.jpg</imgsrc>
<title> Hong Kong 2</title>
<url>http://torchrelay.beijing2008.cn/cn/journey/hongkong/</url>
</imags>
</root>
3. Summary:
Look at the three methods for XML operations (read, write, delete), their initialization statements are the same:
Copy Code code as follows:
Builder = Factory.newdocumentbuilder ();
Document doc=builder.parse (path);
Doc.normalize ();
At first I was going to take this part of the same part and write it in the construction method, but I found in the test that it would always be a mistake to report a null pointer (for a long time);
And just put
Builder = Factory.newdocumentbuilder ();
When you put it in the initialization statement of builder, you can read it well, but when you write or delete it, it will also make an error.
So it was written now, feeling a little messy, but also did not think of any good method, the first posted here, may be useful in the future