Java code
Package com. ylt. mfz. util;
Import java. io. File;
Import java. io. FileWriter;
Import java. io. IOException;
Import java. io. Writer;
Import java. util. Iterator;
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. SAXReader;
Import org. dom4j. io. XMLWriter;
Public class Dom4jDemo {
/**
* Create an xml file
* @ Param fileName
*/
Public void createXml (String fileName)
{
Document document = incluenthelper. createDocument (); // create a Document
Element employees = document. addElement ("employees ");
Element employee = employees. addElement ("employee ");
Element name = employee. addElement ("name"); // Add a subnode
Name. setText ("ddvip"); // Add the Text value. Example: <a> abc </a>
Name. setAttributeValue ("sa", "sa"); // Add attributes. For example, <a item = "item"> </a>
Element sex = employee. addElement ("sex ");
Sex. setText ("m ");
Element age = employee. addElement ("age ");
Age. setText ("29 ");
Try {
Writer fileWriter = new FileWriter (fileName );
XMLWriter xmlWriter = new XMLWriter (fileWriter );
XmlWriter. write (document); // write to file
XmlWriter. close ();
} Catch (IOException e ){
System. out. println (e. getMessage ());
}
}
/**
* Reading xml files
* @ Param fileName
*/
Public void parserXml (String fileName)
{
File inputXml = new File (fileName );
SAXReader saxReader = new SAXReader ();
Try {
Document document = saxReader. read (inputXml); // read the file to the Document
Element employees = document. getRootElement (); // obtain the document root node
// System. out. println (employees );
// For (Iterator I = employees. elementIterator (); I. hasNext ();){
// Element employee = (Element) I. next ();
// System. out. println (employee );
// For (Iterator j = employee. elementIterator (); j. hasNext ();){
// Element node = (Element) j. next ();
// System. out. println (node. getName () + ":" + node. getText () + ":");
// For (Iterator m = node. attributeIterator (); m. hasNext ();)
//{
// Attribute item = (Attribute) m. next ();
// System. out. println (item. getName () + "," + item. getValue ());
//}
//}
This. doublexml (employees );
//}
} Catch (incluentexception e ){
System. out. println (e. getMessage ());
}
System. out. println ("dom4j parserXml ");
}
Private void doublexml (Element ele)
{
For (Iterator I = ele. elementIterator (); I. hasNext ();)
{
Element node = (Element) I. next ();
System. out. println ("node name:" + node. getName ());
If (node. attributes ()! = Null & node. attributes (). size ()> 0)
{
For (Iterator j = node. attributeIterator (); j. hasNext ();)
{
Attribute item = (Attribute) j. next ();
System. out. print ("attribute name:" + item. getName () + "\ t attribute value:" + item. getValue () + "\ n ");
}
}
If (node. getText (). length ()> 0)
{
System. out. println ("node value:" + node. getText ());
}
If (node. elementIterator (). hasNext ())
{
This. doublexml (node );
}
}
}
Public static void main (String [] args ){
Dom4jDemo demo = new Dom4jDemo ();
Demo. createXml ("d:/sa. xml ");
Demo. parserXml ("d:/getchannels [1]. xml ");
}
}
<Pre name = "code" class = "java"> </pre> <pre name = "code" class = "java"> </pre> <pre name = "code "class =" java "> </pre>
Author "jeasonjack"