Java parses and transmits XML format data using JDom

Source: Internet
Author: User

Java uses JDom to parse and transmit XML format data Java uses JDom to parse and process XML data format: the required package jdom-1.1.2.jar1 converts the data into XML format data and transmits Element rootList, firstList, secondItem, thirdItem; // The root Element tag name. rootList = new Element ("root"); // The attribute name and value in the root Element tag. setAttribute ("project", pname); // generate the Doc Document Doc = new Document (rootList); // obtain the root tag in the Document rootList = Doc. getRootElement (); for (int I = 0; I <judges. size (); I ++) {// generate the new Element firstList = new Element ("flayout" ); FirstList. setAttribute ("percent", "percent"); // Add it to the root-level element. addContent (firstList);} XMLOutputter XMLOut = new XMLOutputter (); // converts doc files to String-type XML Format String xmlinfo = XMLOut. outputString (Doc); // remove xmlinfo = xmlinfo from the beginning. replace ("<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> "," "); // Return the encapsulated XML data return xmlinfo; 2. parse the XML in the string for processing // create a new string StringReader read = new StringReader (stadXML ); // create a new input source. The SAX Parser uses the InputSource object to determine how to read the XML input. InputSource source = new InputSource (read ); // create a new SAXBuilderSAXBuilder sb = new SAXBuilder (); String projectName; List <Judgestandard> standIndex = new ArrayList <Judgestandard> (); try {// construct a Document doc = sb through the input source. build (source );/ /Obtain the root Element root = doc. getRootElement (); projectName = root. getAttributeValue ("project"); // obtain the set of all child elements of the root Element. Element et = null; List nodes = root. getChildren (); // Level 1 metric for (int I = 0; I <nodes. size (); I ++) {et = (Element) nodes. get (I); // cyclically obtain the sub-element Judgestandard judge = new Judgestandard (); // obtain the attribute value String fid = et in this element. getAttributeValue ("mainid"); // obtain the number of children of the element List fsize = et. getChildren ();// Level 2 Index for (int j = 0; j <fsize. size (); j ++) {et = (Element) fsize. get (j); // obtain the sub-element et in sequence in a loop. getAttributeValue ("stdid")} Java does not need to include XML documents to be processed when processing XML documents: <? Xml version = "1.0" encoding = "ISO-8859-1"?> <Root> <ip> localhost </ip> <port> 8080 </port> </root> static DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); static DocumentBuilder builder = null; builder = factory. newDocumentBuilder (); // obtain the server root directory Address Document document Document = builder. parse (new File ("src/ip. xml "); Element rootElement = document. getDocumentElement (); NodeList list1 = rootElement. getElementsByTagName ("ip"); NodeList list2 = rootElement. getElementsByTagName ("port"); Element ip = (Element) list1.item (0); Element port = (Element) list2.item (0); String s = ip. getFirstChild (). getNodeValue (). toString () + ":" + port. getFirstChild (). getNodeValue (). toString (); System. out. println (s );

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.