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!