Java fully parse XML format string (jdom parsing)
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 Stringreade
R (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 = Saxbuilder ();
try {//construct a document document DOC = Sb.build (source) from the input source;
root element root = Doc.getrootelement ();
System.out.println (Root.getname ())//outputs the name of the root element (test)//Gets the collection List Jiedian = Root.getchildren () of all child elements of the root element;
Gets the namespaces in the 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);
List Zjiedian = Et.getchildren ();
for (int j=0;j<zjiedian.size (); j + +) {Element Xet = (Element) zjiedian.get (j);
System.out.println (Xet.getname ());
The catch block (Jdomexception e) {//TODO automatically generates a 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 = "<?xml version=\" 1.0\ "encoding=\" gb2312\ "?>" + "<result xmlns=\" http://www.fiorano.com/fesb/act Ivity/dbqueryoninput2/out\ ">" + "<row resultcount=\" 1\ ">" + "<users_id>1001 </users_id>" + "<users_name>wangwei </users_name>" + "<users_group>80 </usErs_group> "+" <users_address>1001 </users_address> "+" </row> "+" <row Resultco Unt=\ "1\" > "+" <users_id>1002 </users_id> "+ <users_name>wangwei </users_name>"
+ "<users_group>80 </users_group>" + "<users_address>1002 number </users_address>" +
"</row>" + "</Result>";
Doc.xmlelements (XML); }
}
The above Java comprehensive Parsing XML format string (Jdom parsing) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.