dom4j simple instance, read through Java code, write XML file

Source: Internet
Author: User

The project will use the following two jar packages

Package com.xml.writer;

Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import Java.io.FileReader;
Import Java.io.InputStreamReader;
Import java.io.UnsupportedEncodingException;
Import java.util.List;

Import org.dom4j.Document;
Import org.dom4j.DocumentException;
Import Org.dom4j.DocumentHelper;
Import org.dom4j.Element;
Import Org.dom4j.Node;
Import Org.dom4j.io.OutputFormat;
Import Org.dom4j.io.SAXReader;
Import Org.dom4j.io.XMLWriter;
Import Org.xml.sax.XMLReader;

public class XmlWriter {

public static void Main (string[] args) {
TODO auto-generated method stubs
XmlWriter ();
XmlReader ();

}

//Known token name, parsing XML
private static void XmlReader () {
//TODO auto-generated method stub

Saxreader reader = new Saxreader ();
Document Doc;
try {
doc = reader.read (new File ("Xml.xml"));
Reads the XML file, gets the Document object
Element root=doc.getrootelement ();//Gets the root node
System.out.println (Root.getname ());
List<element> list=root.elements ();//Gets the child node of the node
for (int i=0;i<list.size (); i++)
{
System.out.println (List.get (i). GetName ());//Node name
System.out.println (List.get (i). GetText ());//Nodes Value
}
} catch (Documentexception e) {
//TODO auto-generated catch block
E.printstacktrace ();
}

}

private static void XmlWriter () {
TODO auto-generated method stubs
Document doc=documenthelper.createdocument ();
Element root=documenthelper.createelement ("student");

Doc.add (root);

Root.addattribute ("id", "1");

Element nameelement=root.addelement ("name");
Element sexelement=root.addelement ("sex");

Nameelement.addtext ("Zhang San");
Sexelement.addtext ("male");

try {
XMLWriter writer=new XMLWriter (New FileOutputStream ("Xml.xml"), Outputformat.createprettyprint ());
Writer.write (DOC);
Writer.flush ();
Writer.close ();
} catch (Exception e) {
TODO Auto-generated catch block
E.printstacktrace ();
}
}

}

dom4j simple instance, read through Java code, write XML file

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.