Two kinds of parsing methods of XML

Source: Internet
Author: User

XML parsing is the use of more skills in daily projects, whether configuration parameters or data can be stored in an XML file, as well as XML as a format for transmitting data. This article describes two basic parsing methods: XML string parsing and XML file parsing.

XML string parsing

Package Com.discover.parse;import Java.util.iterator;import Org.dom4j.document;import org.dom4j.DocumentException; Import Org.dom4j.documenthelper;import org.dom4j.element;/** * @author Regiment Commander * */public Class Xmlstringparser {public S tatic void Main (string[] args) {String xml = "<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>" + "<ro Ot><student><name>jim</name><age>12</age></student> "+" &LT;STUDENT&GT;&L        T;name>jim</name><age>12</age></student></root> ";            try {Document document = Documenthelper.parsetext (XML);            Element root = Document.getrootelement ();            Iterator stuiterator = root.elementiterator ("student");                while (Stuiterator.hasnext ()) {Element student = (Element) Stuiterator.next ();                String name = Student.elementtexttrim ("name");                String age = Student.elementtexttrim ("Age");System.out.println ("Name:" +name+ ", Age:" +age);        }} catch (Documentexception e) {//TODO auto-generated catch block E.printstacktrace (); }    }}
XML file parsing

Package Com.discover.parse;import Java.io.file;import Java.util.iterator;import org.dom4j.document;import Org.dom4j.documentexception;import Org.dom4j.element;import org.dom4j.io.saxreader;/** * @author Regiment Commander * */public Class Xmlfileparser {/** * @param args */public static void main (string[] args) {String Xmlfilepath = Xm        LFileParser.class.getResource (""). GetPath () + "Stuinfo.xml";        File XMLFile = new file (Xmlfilepath);            if (xmlfile.exists ()) {Saxreader reader = new Saxreader ();                try {Document document = Reader.read (xmlfile);                Element root = Document.getrootelement ();                Iterator stuiterator = root.elementiterator ("student");                    while (Stuiterator.hasnext ()) {Element student = (Element) Stuiterator.next ();                    String name = Student.elementtexttrim ("name");                    String age = Student.elementtexttrim ("Age"); SYstem.out.println ("Name:" +name+ ", Age:" +age); }} catch (Documentexception e) {//TODO auto-generated catch block E.printstack            Trace (); }        }    }}

Both of these parsing methods are implemented using the DOM4J, which can be downloaded on the following website

Http://www.dom4j.org/dom4j-1.6.1/

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Two kinds of parsing methods of XML

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.