The dom4j operation xml demo and the dom4j operation xmldemo
Package com.cn. shop. util;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. OutputStreamWriter;
Import java. util. Iterator;
Import java. util. List;
Import org. dom4j. Attribute;
Import org. dom4j. Document;
Import org. dom4j. extends entexception;
Import org. dom4j. DocumentHelper;
Import org. dom4j. Element;
Import org. dom4j. io. OutputFormat;
Import org. dom4j. io. SAXReader;
Import org. dom4j. io. XMLWriter;
/**
*
* @ Author NH
*
*/
Public class XmlUtils {
Public static Document getDocument (){
// 1. Read the xml file to obtain the document Object
SAXReader reader = new SAXReader ();
Document document = null;
Try {
Document = reader. read ("D: \ itext \ 27663.xml ");
} Catch (incluentexception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
// 2. parse xml text
/*
* String xmlFilePath = "D :\\ itext \\ 27663.xml"; try {document =
* Incluenthelper. parseText (xmlFilePath);} catch (incluentexception e)
* {// TODO Auto-generated catch block e. printStackTrace ();} // 3. Pass
* Document document = incluenthelper. createDocument (); Element root =
* Document. addElement ("csdn ");
*/
Return document;
}
Public static void anaXml () throws Exception {
// Read xml text to create a document Object
SAXReader reader = new SAXReader ();
Try {
Document document = reader. read ("D: \ itext \ 27663.xml ");
Element root = document. getRootElement ();
System. out. println (root. getName ());
GetElement (root );
/* ElementMethod (root );*/
/*
* // Obtain a node Element = root. element ("title ");
*
*
* // Obtain the id Attribute of the element. The Node object attribute attr = element. Attribute ("id ");
* // Delete attribute element. remove (attr );
*
* // Add the new attribute element. addattrir ("author", "author ");
*
* // Add the new node Element newElement = root. addElement ("where ");//
* Set newElement. setText ("Beijing People's press, Tianjin People's University Press") for the new node ");
*
* // Obtain the where element Node object Element author =
* Element. element ("where"); // Delete the element Node boolean flag =
* Element. remove (author); // return true. The code is successfully deleted; otherwise, it fails.
* System. out. println (flag); // Add the CDATA Region
* Element. addCDATA ("A Dream of Red Mansions, is a love novel."); // write it to a new file using writer (document );
*/
} Catch (incluentexception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
/*
*
* All node content
*/
Public static void getElement (Element root ){
// Obtain all attribute nodes of the current node
List <Attribute> as = root. attributes ();
For (Attribute a: ){
System. out. println ("Name of the current property node:" + a. getName ());
/*
* System. out. println ("content of the current attribute node:" + a. getText ());
*
* System. out. println ("Current attribute node value:" + a. getValue ());
*/
}
If (! Root. getTextTrim (). equals ("")){
System. out. println ("text content:" + root. getText ());
}
Iterator <Element> el = root. elementIterator ();
While (el. hasNext ()){
// Obtain a subnode object
Element e = el. next ();
// Traverse subnodes
GetElement (e );
}
}
/**
* Describes the use of the Element method and elements Method in element.
*
* @ Param node
*/
Public static void elementMethod (Element node ){
// Obtain the element node with the child node name travel to the West.
Element e = node. element ("info ");
// Obtain the element node with the child node as chapter in the Travel Notes to the West (you can see that only the first author element node can be obtained)
Element author = e. element ("classification ");
System. out. println (e. getName () + "----" + author. getText ());
// Obtain the nodes with the name of classification in the element node of travel to the West.
List <Element> authors = e. elements ("classification ");
For (Element aut: authors ){
System. out. println (aut. getText ());
}
// Obtain the child nodes of all elements of the element node of Travel Notes to the west.
List <Element> elements = e. elements ();
For (Element el: elements ){
System. out. println (el. getText ());
}
}
/**
* Write the document object to a new file.
*
* @ Param document
* @ Throws Exception
*/
Public static void writer (Document document) throws Exception {
// Compact format
// OutputFormat format = OutputFormat. createCompactFormat ();
// Typographical indent format
OutputFormat format = OutputFormat. createPrettyPrint ();
// Set the Encoding
Format. setEncoding ("UTF-8 ");
// Create an XMLWriter object, specifying the Write File and encoding format
/*
* XMLWriter writer = new XMLWriter (new OutputStreamWriter (new
* FileOutputStream (new File ("src // a. xml"), "UTF-8"), format );
*/
File file = new File ("c: // index // master. xml ");
FileOutputStream fos = new FileOutputStream (file );
OutputStreamWriter osw = new OutputStreamWriter (fos, "UTF-8 ");
XMLWriter writer = new XMLWriter (osw );
// Write
Writer. write (document );
// Write now
Writer. flush ();
// Close the operation
Writer. close ();
}
// The following code is used to convert strings to xml instances.
Public void test () throws Exception {
// Create a saxreader object
SAXReader reader = new SAXReader ();
// Read an object and convert it into a Document Object
Document document = reader. read (new File ("src // c. xml "));
// Obtain the root element
Element root = document. getRootElement ();
// Convert the document string
String docXmlText = document. asXML ();
System. out. println (docXmlText );
System. out. println ("---------------------------");
// Csdn element label root conversion content
String rootXmlText = root. asXML ();
System. out. println (rootXmlText );
System. out. println ("---------------------------");
// Obtain the content in the java element label
Element e = root. element ("java ");
System. out. println (e. asXML ());
}
/**
* Create a document object to add a node element to the document Object and convert it to an xml file.
*
* @ Throws Exception
*/
Public void test2 () throws Exception {
Document document = incluenthelper. createDocument (); // create a root node
Element root = document. addElement ("csdn ");
Element java = root. addElement ("java ");
Java. setText ("java class ");
Element ios = root. addElement ("ios ");
Ios. setText ("ios class ");
Writer (document );
}
/**
* Convert a text string to a Document Object
*
* @ Throws Exception
*/
Public void test1 () throws Exception {
String text = "<csdn> <java> Java class </java> <net> Net class </net> </csdn> ";
Document document = incluenthelper. parseText (text );
Element e = document. getRootElement ();
System. out. println (e. getName ());
Writer (document );
}
/**
* Write the document object to a new file.
*
* @ Param document
* @ Throws Exception
*/
Public void writer1 (Document document) throws Exception {
// Compact format
// OutputFormat format = OutputFormat. createCompactFormat ();
// Typographical indent format
OutputFormat format = OutputFormat. createPrettyPrint ();
// Set the Encoding
Format. setEncoding ("UTF-8 ");
// Create an XMLWriter object, specifying the Write File and encoding format
// XMLWriter writer = new XMLWriter (new FileWriter (new
// File ("src // a. xml"), format );
XMLWriter writer = new XMLWriter (new OutputStreamWriter (new FileOutputStream (new File ("src // c. xml"), "UTF-8 "),
Format );
// Write
Writer. write (document );
// Write now
Writer. flush ();
// Close the operation
Writer. close ();
}
Public static void main (String [] args ){
Try {
AnaXml ();
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}