Java parsing XML format string (Jdom parsing)

Source: Internet
Author: User

Importjava.io.IOException;ImportJava.io.StringReader;Importjava.util.List;Importorg.jdom.Document;Importorg.jdom.Element;Importorg.jdom.JDOMException;ImportOrg.jdom.Namespace;ImportOrg.jdom.input.SAXBuilder;ImportOrg.xml.sax.InputSource; Public classDuxmldoc { PublicList xmlelements (String xmldoc) {//Create a new stringStringReader Read =NewStringReader (xmldoc); //Creating a new input source The SAX parser uses the InputSource object to determine how to read the XML inputInputSource Source =NewInputSource (read); //Create a new SaxbuilderSaxbuilder SB =NewSaxbuilder (); Try {            //constructs a document from the input sourceDocument doc =sb.build (source); //the root element takenElement root =doc.getrootelement (); System.out.println (Root.getname ());//the name of the output root element (test)//gets the collection of all child elements of the root elementList Jiedian =Root.getchildren (); //get namespaces in XML (undefined in XML is not writable)Namespace NS =Root.getnamespace (); Element et=NULL;  for(intI=0;i<jiedian.size (); i++) {et= (Element) jiedian.get (i);//loops get the child elements in turnSystem.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(intJ=0;j<zjiedian.size (); j + +) {Element Xet=(Element) Zjiedian.get (j);            System.out.println (Xet.getname ()); }        } Catch(jdomexception e) {//TODO automatically generates catch blocksE.printstacktrace (); } Catch(IOException e) {//TODO automatically generates catch blocksE.printstacktrace (); }        return NULL; }     Public Static voidMain (string[] args) {Duxmldoc doc=NewDuxmldoc (); String XML= "<?xml version=\" 1.0\ "encoding=\" gb2312\ "?>" + "<result xmlns=\" Http://www.fiorano.com/fesb/activity/DBQ Ueryoninput2/out\ ">" + "<row resultcount=\" 1\ ">" + "<users_id>1001 &LT;/USERS_ID&G t; " + "<users_name>wangwei </users_name>" + "<users_group>80 </users_gr  Oup> "+" <users_address>1001 </users_address> "+" </row> "+" <row Resultcount=\ "1\" > "+" <users_id>1002 </users_id> "+" <users_name>wangwe I </users_name> "+" <users_group>80 </users_group> "+" <users_addres s>1002 </users_address> "+" </row> "+" </Result> ";    Doc.xmlelements (XML); }}

Java parsing XML format string (Jdom parsing)

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.