Java comprehensive parsing XML format string (jdom parsing) _java

Source: Internet
Author: User

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.

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.