Java uses jdom to generate xml format files, and javajdom to generate xml

Source: Internet
Author: User

Java uses jdom to generate xml format files, and javajdom to generate xml

The tool used to generate xml in this article is jdom. jar, as follows:
Link: https://eyun.baidu.com/s/3slyHgnj password: 0TXF
Import java. io. FileOutputStream;
Import java. io. IOException;
Import org. jdom. Document;
Import org. jdom. Element;
Import org. jdom. JDOMException;
Import org. jdom. output. Format;
Import org. jdom. output. XMLOutputter;
Public class Java2XMLNew {
Public void BuildXMLDoc () throws IOException, JDOMException {
// Create a root node and set its attributes;
Element root = new Element ("book ");
// Add the root node to the document;
Document Doc = new Document (root );
Root. addContent (new Element ("java"). setText ("java "));
Root. addContent (new Element ("hadoop"). setText ("hadoop "));
Root. addContent (new Element ("mysql"). setText ("mysql "));
Root. addContent (new Element ("AJAX"). setText ("AJAX "));
Root. addContent (new Element ("Jquery"). setText ("Jquery "));
Root. addContent (new Element ("oracle"). setText ("oracle "));
Root. addContent (new Element ("mongodb"). setText ("mongodb "));
// Output the books. xml file;
// Indent the xml file
Format format = Format. getPrettyFormat ();
XMLOutputter XMLOut = new XMLOutputter (format );
XMLOut. output (Doc, new FileOutputStream ("D:/books. xml "));
}
Public static void main (String [] args ){
Try {
Java2XMLNew j2x = new Java2XMLNew ();
System. out. println ("generating xml file ");
J2x. BuildXMLDoc ();
} Catch (Exception e ){
E. printStackTrace ();
}
System. out. println ("D:/books. xml file generated successfully ");
}
}
The format of the generated document is the same as that of the image above!

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.