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