[Java-XML] Parse XML strings using JDOM (non-XML document)

Source: Internet
Author: User

Package CMO. shubai. JDOM;

Import java. Io. ioexception;
Import java. Io. stringreader;
Import java. util. List;

Import org. JDOM. Document;
Import org. JDOM. element;
Import org. JDOM. jdomexception;
Import org. JDOM. namespace;
Import org. JDOM. Input. saxbuilder;
Import org. xml. Sax. inputsource;

Public class duxmldoc {
Public list xmlelements (string xmldoc ){
// Create a new string
Stringreader READ = new stringreader (xmldoc );
// Create a new input source. The SAX Parser uses the inputsource object to determine how to read the XML input.
Inputsource source = new inputsource (read );
// Create a new saxbuilder
Saxbuilder sb = new saxbuilder ();
Try {
// Construct a document using the input source
Document Doc = sb. Build (source );
// Obtain the root element.
Element root = Doc. getrootelement ();
System. Out. println (root. getname (); // name of the output root element (test)
// Obtain the set of all child elements of the root element.
List jiedian = root. getchildren ();
// Obtain the namespace in XML (The namespace is not defined in XML and can not be written)
Namespace NS = root. getnamespace ();
Element ET = NULL;
For (INT I = 0; I et = (element) jiedian. Get (I); // The subelement is obtained in a loop.
/**//*
* When no namespace is defined
* Et. getchild ("users_id"). gettext ();
* Et. getchild ("users_address", NS). gettext ()
*/
System. Out. println (ET. getchild ("users_id", NS). gettext ());
System. Out. println (ET. getchild ("users_address", NS). gettext ());
}
/**//*
* For the name of the child element to be removed
*/
ET = (element) jiedian. Get (0 );
List zjiedian = ET. getchildren ();
For (Int J = 0; j element xet = (element) zjiedian. Get (j );
System. Out. println (xet. getname ());
}
} Catch (jdomexception e ){
// Todo automatically generates catch Blocks
E. printstacktrace ();
} Catch (ioexception e ){
// Todo automatically generates catch Blocks
E. printstacktrace ();
}
Return NULL;
}
Public static void main (string [] ARGs ){
Duxmldoc Doc = new duxmldoc ();
String xml = "" +
"+
"+
"1001" +
"Wangwei" +
"80" +
"1001" +
"+
"+
"1002" +
"Wangwei" +
"80" +
"1002" +
"+
"";
Doc. xmlelements (XML );
}
}

The preceding example is slightly different from the actual one-this example is mainly used to demonstrate how JDOM parses XML strings.

JDOM processes XML files with namespace

Recently encountered some problems in XML file reading during development-JDOM Technology reading
1
2
3
4 1001
5 wangwei
6 80
1001
8
9
10 1002
11 wangwei
12 80
13 1002
14
15
16 1003
17 wangwei
18 80
19, 1003
20
21
22 1004
23 wangwei
24 80
25, 1004
26
27

CodeXmlns = "http://www.fiorano.com/fesb/activity/dbqueryoninput2/out”>
It is the namespace of the XML file. when reading the element value of this document, you must specify its namespace. Otherwise, a null pointer exception occurs in Java. Lang. nullpointerexception.
Actually executed code

Public class duxmltest2 {
Public void loadxmldoc (){
Fileinputstream Fi = NULL;
Try {
FI = new fileinputstream ("test. xml ");
Saxbuilder sb = new saxbuilder ();
Try {
Document Doc = sb. Build (FI );
Element root = Doc. getrootelement ();
Namespace NS = root. getnamespace ();
List jiedian = root. getchildren ();
Element ET = NULL;
For (INT I = 0; I et = (element) jiedian. Get (I );
System. Out. println (ET. getchild ("users_id", NS). gettext ());
}
} Catch (jdomexception e ){
// Todo automatically generates catch Blocks
E. printstacktrace ();
} Catch (ioexception e ){
// Todo automatically generates catch Blocks
E. printstacktrace ();
}
} Catch (filenotfoundexception e ){
// Todo automatically generates catch Blocks
E. printstacktrace ();
}
}
Public static void main (string [] ARGs ){
Duxmltest2 dt = new duxmltest2 ();
DT. loadxmldoc ();
}

 

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.