Java and JDOM for XML file reading (netbeans Development)

Source: Internet
Author: User
Tags netbeans

The code of this project comes from the Internet. This article only gives a brief introduction to the process of running this case in netbeans6.1.

1) In this example, JDOM is used to read the content of an XML document. You need to download JDOM. jar, www.jdom.org.
(Download jdom1.1.zip)

2) create a new Java application in netbeans 6.1 and name it xml_javatest. Load the downloaded jar package to the library.

3) create a new package named xml_javatest

3) Prepare an XML file: Create an XML file under the root directory of the project and name it my. xml.

Content:

<? XML version = "1.0" encoding = "gb2312"?>
<Urlinfo>
<Blog_url>
<Url_name> http://hi.baidu.com/Lovesah </url_name>
</Blog_url>
</Urlinfo>

(Note: XML files must be directly stored in the project directory, rather than generated in netbeans)

4) Write a Java program to read the XML document and create a new Java program named test2.java.

Code:

/*
* To change this template, choose tools | templates
* And open the template in the editor.
*/

Package xml_javatest;

/**
*
* @ Author Crystal
*/

Import java. Io .*;
Import org. JDOM .*;
Import org. JDOM. Input .*;
Import org. JDOM. Output .*;
Import java. util .*;

Public class Test2 {
Public Test2 (){
}

Public void readxml (){
Try {
Saxbuilder sb = new saxbuilder ();
Document Doc = sb. Build (New fileinputstream ("My. xml "));
Element El = Doc. getrootelement ();
List ls = El. getchildren ("blog_url"); // obtain the specified level-2 menu
Iterator it = ls. iterator ();
While (it. hasnext ()){
Element sub_e = (element) it. Next ();
Element sub_e_2 = sub_e.getchild ("url_name"); // obtain the specified tag Value
System. Err. println (sub_e_2.getname () + "" + sub_e_2.gettext ());
}
} Catch (exception e ){
E. printstacktrace ();
}
}

Public static void main (string [] ARGs ){
Test2 Test2 = new Test2 ();
Test2.readxml ();
}
}

5) run the file and the test result is:

Note: The above is the process of testing JDOM to read XML file content in netbeans. I hope the steps are described in detail ....

 

Note: The above content comes from the Internet and is not jointly and severally liable.

Article transferred from: http://blog.csdn.net/meteorlWJ/archive/2008/02/05/2084702.aspx

 

 

 

 

 

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.