Java parses XML format strings (JDOM parsing) and xmljdom
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 InputSource. Object To determine how to read the XML input InputSource source = new InputSource (read); // create a new SAXBuilder sb = new SAXBuilder (); try {// construct a Document doc = sb through the input source. build (source); // obtain the root 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 (); // get the Namespace 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); // cyclically obtain the sub-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 () ;}} catch (jdom1_tio N e) {// TODO automatically generates catch Block e. printStackTrace ();} catch (IOException e) {// TODO automatically generates catch Block e. printStackTrace ();} return null;} public static void main (String [] args) {DuXMLDoc doc = new DuXMLDoc (); String xml = "<? Xml version = \ "1.0 \" encoding = \ "gb2312 \"?> "+" <Result xmlns = \ "success"> "+" <row resultcount = \ "1 \"> "+" <users_id> 1001 </users_id> "+" <users_name> wangwei </users_name> "+" <users_group> 80 </users_group> "+" <users_address> Nov 1001 </users_address> "+" </row> "+" <row resultcount = \ "1 \"> "+" <users_id> 1002 </users_id> "+" <users_name> wangwei </users_name> "+" <users_group> 80 </users_group> "+" <users_address> Nov 1002 </users_address> "+" </row> "+" </Result> "; doc. xmlElements (xml );}}