[JAVA100 example]071, using Jdom to manipulate XML

Source: Internet
Author: User
Tags character set

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 ();
}
}

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.