import org.jdom.*;
import org.jdom.output.*;
import java.io.*;
import java.util.List;
/**
* <p>title: Use Jdom to manipulate XML files. </p>
* <p>description: </p>
* <p>copyright:copyright (c) 2003</p>
* <p> Filename:jdomoption.java</p>
* @version 1.0
*/
public class Jdomoption {
/**
*<br> Method Description: Construct Builder, implement construct XML file, delete element
*<br> input parameter:
*<br> return type:
*/
Public jdomoption () {
string[] Strchapter = { "Basic knowledge", "Graphical Interface", "Graphics processing", "I/O operation", "Network Programming", "Database"};
Element elmtroot = new Element ("Article");
Document Docjdom = new document (Elmtroot);
//new element
for (int i=0; i<6; i++) {
///declaration of new Element
Element elmtchapter = newer ("Chapter");
Add Content
Elmtchapter.addcontent (strchapter[i]);
Constructs the attribute and adds it to the element
attribute a=new attribute ("Sort", new Integer (i). toString ());
Elmtchapter.addattribute (a);
//Add elements under root node
ELMTROOT.ADdcontent (Elmtchapter);
}
//?h In addition to the 4th element
List lstchapter = Elmtroot.getchildren ("Chapter");
Lstchapter.remove (4);
//?? Out
Outputxml (docjdom, "Myjdom.xml");
}
/**
*<br> Method Description: Output XML file
*<br> input parameter: Document docxml XML file contents
*<br> input parameters: String strFileName output file name
*<br> return type:
*/
private void Outputxml (document Docxml, String strFileName) {
// Use jdom XML output
Xmloutputter FMT = new Xmloutputter ();
try {
//declaration uses the GB2312 character set
fmt.setencoding ("GB2312");
You can wrap
Fmt.setnewlines (true);
Output file Object
FileWriter fwxml = new FileWriter (strFileName);
Fmt.output (Docxml, fwxml);
Fwxml.close ();
} catch (IOException e) {
E.printstacktrace ();
}
/**
*<br> Method Description: Main method
*<br> input parameter:
*<br> return type:
*/
public static void main (STR Ing[] args {
Jdomoption jpt = new Jdomoption ();
}
}