Article Source: http://blog.sina.com.cn/s/blog_700dec940100spox.html
He's using Jdom.jar parsing XML files.
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) {
To create a new string
StringReader read = new StringReader (xmldoc);
Create a new input source The SAX parser will use 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 from an input source
Document doc = sb.build (source);
The root element taken
Element root = Doc.getrootelement ();
System.out.println (Root.getname ());//output root element name (test)
Gets the collection of all child elements of the root element
List Jiedian = Root.getchildren ();
Get namespaces in XML (not defined in XML)
Namespace ns = Root.getnamespace ();
Element et = null;
for (int i=0;i<jiedian.size (); i++) {
ET = (Element) jiedian.get (i);//loop to get child element
System.out.println (Et.getchild ("users_id", NS). GetText ());
System.out.println (Et.getchild ("users_address", NS). GetText ());
}
ET = (Element) jiedian.get (0);