Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.util.Scanner;
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 Javax.xml.xpath.XPath;
Import javax.xml.xpath.XPathConstants;
Import javax.xml.xpath.XPathExpressionException;
Import Javax.xml.xpath.XPathFactory;
Import org.w3c.dom.Document;
Import org.w3c.dom.Element;
Import Org.w3c.dom.Node;
Import org.w3c.dom.NodeList;
Import org.xml.sax.SAXException;
public class Domsync {
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 ();
Scanner scan = new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Input file path");
StringBuffer sb = new StringBuffer ();
Scanner in = new Scanner (system.in);
String next = null;
do {
Next = In.next ();
Sb.append (next);
} while (!next.equals ("End"));
In.close ();
Document Xmldoc=db.parse (New File (Sb.tostring (). ReplaceAll ("End", "")));
Root=xmldoc.getdocumentelement ();
---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);
---Create a new book to complete----
//---Make some changes to Harry Potter below. ----
//---query for Harry Potter----
Thebook= (Element) selectSingleNode ("/l2vpn/vpwsinstances/vpwsinstance", root);
SYSTEM.OUT.PRINTLN ("---search for" vpwsinstance "----");
Output (Thebook);
//---Modify the price of the book at this time-----
//Thebook.getelementsbytagname ("Prices"). Item (0). Settextcontent ("15");// getElementsByTagName returns the nodelist, so keep the item (0). In addition, getElementsByTagName ("price") is equivalent to the XPath ".//price".
//SYSTEM.OUT.PRINTLN ("---at this time revise the price of this book----");
//output (Thebook);
//---also want to add an attribute ID with a value of B01----
Element newattr = xmldoc.createelement ("description");
Text t = xmldoc.createtextnode ("<description/>");
//Newattr.set
//Newattr.appendchild (t);
Thebook.appendchild (newattr);
SYSTEM.OUT.PRINTLN ("---also want to add an attribute ID with a value of B01----");
Output (Thebook);
//---to the completion of the Harry Potter amendment. ----
---Want to delete the book "Kingdoms" with the id attribute----
thebook= (Element) selectSingleNode ("/l2vpn/vpwsinstances/vpwsinstance/instancename", root);
SYSTEM.OUT.PRINTLN ("---to delete the Kingdoms book with the id attribute----");
Output (Thebook);
Thebook.getparentnode (). removechild (Thebook);
SYSTEM.OUT.PRINTLN ("---the deleted XML----");
Output (xmldoc);
---Delete all books with a price below 10----
NodeList somebooks=selectnodes ("/books/book[price<10]", root);
SYSTEM.OUT.PRINTLN ("---to delete all books with a price below 10---");
SYSTEM.OUT.PRINTLN ("---the Qualifying book has" +somebooks.getlength () + "Ben. ---");
for (int i=0;i<somebooks.getlength (); i++) {
Somebooks.item (i). Getparentnode (). RemoveChild (Somebooks.item (i));
// }
Output (xmldoc);
SaveXML ("L2vpn.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's 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) {//Find node and return 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;
}
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) {//Outputs 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 (New FileOutputStream (FileName));
Transformer.transform (source, result);
} catch (Transformerconfigurationexception e) {
E.printstacktrace ();
} catch (Transformerexception e) {
E.printstacktrace ();
} catch (FileNotFoundException e) {
E.printstacktrace ();
}
}
}
http://blog.csdn.net/smildlzj/article/details/1548713
Dom Processing configuration file _ to be perfected