Java comprehensively parses XML format strings (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 StringReader(xmlDoc); //Create a new input source The SAX parser will use the InputSource object to determine how to read XML input InputSource source = new InputSource(read); //Create a new SAXBuilder SAXBuilder sb = new SAXBuilder(); try { //Construct a Document doc = sb.build(source); //Get the root element Element root = doc.getRootElement(); System.out.println(root.getName()); //Output the name of the root element (test) //Get the set of all child elements of the root element List jiedian = root.getChildren(); //Get the namespace in XML (not defined in XML can be written) Namespace ns = root.getNamespace(); Element et = null; for(int i=0;i<jiedian.size();i++){ et = (Element) jiedian.get(i);//Loop to obtain the child elements 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 (JDOMException 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=/"http://www.fiorano.com/fesb/activity/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 resultcount=/"1/">"+ "<users_id>1002 </users_id>"+ "<users_name>wangwei </users_name>"+ "<users_group>80 </users_group>"+ "<users_address>1002</users_address>"+ "</row>"+ "</Result>"; doc.xmlElements(xml); }}The above article on Java comprehensive parsing XML format string (JDOM parsing) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.