Simple XML example of JDOM operations

Source: Internet
Author: User
Tags xml example
The openness of Java attracts many companies and individuals to constantly improve Java's performance. JDOM is the creation result of two famous java developers and authors, Brett McLaughlin and Jason hunter. It is committed to establishing a complete Java-based solution through Java Code To access, operate, and output XML data.
JDOM can be downloaded from the http://jdom.com website, the latest version is beta9.0.

1. Use JDOM to create an XML document
We want to create an XML document in the following format: 1.xml
<? XML version = "1.0" encoding = "gb2312"?>
<Personal Data>
<Name ID number = "2222"> Zhang 'er </Name>
<Age> 20 </age>
</Personal Data>

First, we create an example of the root element and document to add the root element to the document:
Element root = new element ("Personal Data ");
Document Doc = new document (Root );

ADD child elements:
Element name = new element ("name ");
Name. setattribute (new attribute ("ID number", "2222 "));
Name. addcontent ("Zhang 'er ");
Root. addcontent (name );
Element age = new element ("Age ");
Age. addcontent ("20 ");
Root. addcontent (AGE );

Because the addcontent () method returns the element type, the above Code can also be written as follows:
Root. addcontent (new element ("name"). addcontent (""). setattribute ("ID", "2222 "));
Root. addcontent (new element ("Age"). addcontent ("20 "));

Use fileoutputstream to generate XML text
Try
{
String ident = "; // The child element is indented with two spaces
Boolean isnew = true; // empty rows between elements
String cset = "gb2312"; // encoding, displaying Chinese Characters
Xmloutputter extends Uter = new xmloutputter (", true, cset );
Outer. Output (Doc. New fileoutputstream ("1. xml "));
} Catch (ioexception E)
{
E. printstacktrace ();
}
The above code is used to generate the XML page as shown above.

Get the corresponding value from 1. xml:
Use saxbuilder to analyze 1. XML syntax
Try
{
Saxbuilder sb = new saxbuilder ();
Document mydoc = sb. Build (New fileinputstream ("1. xml "));
} Catch (jdomexception E)
{
E. printstacktrace ();
} Catch (nullpointerexception E)
{
E. printstacktrace ();
}

Access child elements
Element another = mydoc. getrootelement (); // obtain the root element first.
Element namee = root. getchild ("name ");
System. Out. println (namee. gettext ());

Delete sub-elements
Boolean Re = another. removechild ("name");
// after deletion, remember to write the document again

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.