Java Jdom parsing CDATA content

Source: Internet
Author: User

Package Com;import Java.io.ioexception;import java.io.stringreader;import java.util.list;import Org.jdom.CDATA; Import Org.jdom.comment;import org.jdom.doctype;import Org.jdom.document;import Org.jdom.element;import Org.jdom.entityref;import Org.jdom.jdomexception;import Org.jdom.processinginstruction;import Org.jdom.Text; Import Org.jdom.input.saxbuilder;public class Test {public static void main (string[] args) {String xml = "<            ? xml version=\ "1.0\" encoding=\ "utf-8\"?> "+" <SrvCont> "+" <SrvRoot> "+ "<! [cdata["+" <?xml version=\ "1.0\" encoding=\ "utf-8\"?> "+" <item><itemid>seqi Nit</itemid><itemvalue>20161126blus3110000001</itemvalue></item> "+" <Item>< Itemid>content</itemid><itemvalue> test Content </ItemValue></Item> "+" <item><item Id>resion</itemid><itemvalue> Test </itemvalUe></item> "+"]]></srvroot></srvcont> ";      Saxbuilder builder = new Saxbuilder ();          try {Document document = Builder.build (new StringReader (XML));          Element root = Document.getrootelement ();          Element data = Root.getchild ("Srvroot"); Reading the mixed content of an XML element and iterate//the result list. This list object can contains any of the/following objects:comment, Element, CDATA, DocType,//Proc          Essinginstruction, EntityRef and Text.          List content = Data.getcontent ();        String result = "";                  for (Object o:content) {if (o instanceof Comment) {Comment Comment = (Comment) o;              System.out.println ("Comment =" + Comment);                  } else if (o instanceof Element) {element element = (element) O; System.out.println ("element =" + Element);             } else if (o instanceof CDATA) {CDATA CDATA = (CDATA) o;                result = Cdata.gettext ();             System.out.println ("CDATA =" + result);                  } else if (o instanceof DocType) {DocType DocType = (DocType) o;              System.out.println ("DocType =" + DocType);                  } else if (o instanceof processinginstruction) {processinginstruction pi = (processinginstruction) o;              System.out.println ("PI =" + pi);                  } else if (o instanceof EntityRef) {EntityRef EntityRef = (EntityRef) o;              System.out.println ("EntityRef =" + EntityRef);                  } else if (o instanceof text) {text text = (text) o;              System.out.println ("text =" + text);      }}} catch (Jdomexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace ();      }  }} 

  

Java Jdom parsing CDATA content

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.