Java basics-simple XML example of JDOM operations

Source: Internet
Author: User
Tags xml example
Java basics-a simple example of JDOM operations-general Linux technology-Linux programming and kernel information. The following is a detailed description. 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, use 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

<个人资料>
<姓名 id号="”2222”"> Zhang 'er
<年龄> 20


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 outer = 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 child element
Boolean re = another. removeChild ("name ");
// After deletion, remember to write the document again
Related Article

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.