Share very useful Java programs (critical code) (vi)---parse/read XML files (important)

Source: Internet
Author: User

Original: Share very useful Java program (key Code) (vi)---parse/read XML file (important)

XML file <?xml version= "1.0"?>  <students>      <student>          <name>John</name>          <grade>B</grade>          <age>12</age>      </student>      <student>          <name>Mary</name>          <grade>A</grade>          <age>11</age>      </student >      <student>          <name>Simon</name>          <grade>A</grade>          <age >18</age>      </student>  


Code for Java parsing:

Package net.viralpatel.java.xmlparser;   Import Java.io.File;  Import Javax.xml.parsers.DocumentBuilder;      Import Javax.xml.parsers.DocumentBuilderFactory;   Import org.w3c.dom.Document;   Import org.w3c.dom.Element;   Import Org.w3c.dom.Node;     Import org.w3c.dom.NodeList; public class Xmlparser {public void Getallusernames (String fileName) {try {documentbuilderfactory             DBF = Documentbuilderfactory.newinstance ();          Documentbuilder db = Dbf.newdocumentbuilder ();               File File = new file (fileName);            if (file.exists ()) {Document doc = db.parse (file);                     Element Docele = Doc.getdocumentelement ();                         Print root element of the document SYSTEM.OUT.PRINTLN ("root element of the document:" + Docele.getnodename ());       NodeList studentlist = docele.getelementsbytagname ("student"); Print total student elements in document SYSTEM.OUT.PRINTLN ("Total Students: "+ studentlist.getlength ()); if (studentlist! = null && studentlist.getlength () > 0) {for (int i = 0; i < studentlist.getlength (); i++                         ) {Node node = Studentlist.item (i);     if (node.getnodetype () = = Node.element_node) {System.out.println ("=====================");                              Element e = (element) node; NodeList NodeList = e.getelementsbytagname ("name");                    System.out.println ("Name:" + nodelist.item (0). Getchildnodes (). Item (0). Getnodevalue ());   NodeList = E.getelementsbytagname ("Grade"); System.out.println ("Grade:" + nodelist.item (0). Getchildnodes (). Item (0). Getnodevalue ()); nodeList =    E.getelementsbytagname ("Age");             System.out.println ("Age:" + nodelist.item (0). Getchildnodes (). Item (0). Getnodevalue ());        }}} else {system.exit (1);        }}} catch (Exception e) {System.out.println (e); }     } public static void Main (string[] args) {xmlparser parser = new Xmlparser ();     }parser.getallusernames ("C:\\test.xml"); }  } }

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

Share very useful Java programs (critical code) (vi)---parse/read XML files (important)

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.