Use dom4j to create XML and parse XML

Source: Internet
Author: User

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"
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.