Dom4J parses xml

Source: Internet
Author: User

Package com. shengsiyuan. dom4j; import java. io. file; import java. util. iterator; import java. util. list; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import org. dom4j. document; import org. dom4j. element; import org. dom4j. io. DOMReader; import org. dom4j. io. SAXReader; public class Test2 {public static void main (String [] args) throws Exception {SAXReader saxReader = new SAXReader (); Document doc = saxReader. read (new File ("student2.xml"); Element root = doc. getRootElement (); System. out. println ("root element:" + root. getName (); List childList = root. elements (); System. out. println (childList. size (); List childList2 = root. elements ("hello"); System. out. println (childList2.size (); Element first = root. element ("hello"); System. out. println (first. attributeValue ("age"); for (Iterator iter = root. elementIterator (); iter. hasNext ();) {Element e = (Element) iter. next (); System. out. println (e. attributeValue ("age");} System. out. println ("---------------------------"); DocumentBuilderFactory dbf = DocumentBuilderFactory. newInstance (); DocumentBuilder db = dbf. newDocumentBuilder (); org. w3c. dom. document document = db. parse (new File ("student2.xml"); DOMReader domReader = new DOMReader (); // convert the Document of JAXP to DocumentDocument d = domReader of dom4j. read (document); Element rootElement = d. getRootElement (); System. out. println (rootElement. getName ());}}

Related Article

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.