Dom parsing XML document

Source: Internet
Author: User
Package COM. softeem. XML. util; import Java. io. file; import Java. util. hashmap; import Java. util. map; import javax. XML. parsers. documentbuilder; import javax. XML. parsers. documentbuilderfactory; import Org. w3C. dom. *; import COM. softeem. XML. dto. classdto; import COM. softeem. XML. dto. collegedto; import COM. softeem. XML. dto. studentdto; public class domparsexml {public static void main (string [] ARGs) throws exception {s Tring Path = "D:/stus. XML "; collegedto c = parsexml (PATH); // obtain information and print system. out. println (C. getclasses (). get ("Family Planning Class 1 "). getstus (). get ("20111114 "). getstuname ();} public static collegedto parsexml (string path) {collegedto college = NULL; try {// Dom parsing // ① load the parsed XML file // use the factory class to create the documentbuilder Class Object documentbuilderfactory factory = documentbuilderfactory. newinstance (); documentbuilder builder = factory. newdocumentbuilde R (); // call the parse method in the documentbuilder class. loading an XML file // DOM object is equivalent to an XML file document dom = builder. parse (new file (PATH); // ② extract the data of the XML file loaded in the DOM object sequentially. // getdocumentelement () method in the document class: get the root Tag Element root = Dom of the XML file. getdocumentelement (); // 1. get the getattribute method in the element property // element class: Get the attribute value from the current element object element based on the attribute name // system. out. println (root. gettagname (); // nodelist NS = root. getelementsbytagname ("class"); *********** string cid = Root. getattribute ("ID"); string cname = root. getattribute ("name"); // 2. obtain the sub-tag nodelist nodes = root of the element. getchildnodes (); // system. out. println (nodes. getlength (); Map <string, classdto> classes = new hashmap <string, classdto> (); For (INT I = 0; I <nodes. getlength (); I ++) {node = nodes. item (I); string nodename = node. getnodename (); // string nodevalue = node. getnodevalue (); // int nodetype = node. getnodety PE (); // return the short variable if ("class ". equals (nodename) {// namednodemap map = node. getattributes (); // node ATTR = map. getnameditem ("classname"); // string classname = ATTR. getnodevalue (); string classname = node. getattributes (). getnameditem ("classname "). getnodevalue (); string cnum = node. getattributes (). getnameditem ("classnum "). getnodevalue (); // obtain the node's subnode nodelist NS2. getchildnodes (); Map <string, studentdto> S Tus = new hashmap <string, studentdto> (); For (Int J = 0; j <ns2.getlength (); j ++) {node n = ns2.item (j ); if ("student ". equals (N. getnodename () {string snum = n. getattributes (). getnameditem ("stunum "). getnodevalue (); string sname = n. getattributes (). getnameditem ("stuname "). getnodevalue (); string ssex = n. getattributes (). getnameditem ("stusex "). getnodevalue (); string sage = n. getattributes (). getnameditem (" Stuage "). getnodevalue (); // put dtostudentdto Stu = new studentdto (snum, sname, ssex, integer. parseint (SAGE); stus. put (snum, Stu); // put a stu} classdto linoleic = new classdto (classname, integer. parseint (cnum), stus); classes. put (classname, CIA) ;}} college = new collegedto (integer. parseint (CID), cname, classes);} catch (exception e) {system. err. println ("error! ") ;}Return college ;}}
<! -- Stus. xml file --> <? XML version = "1.0" encoding = "UTF-8"?> <College ID = "1001" name = ""> <class classname = "Family Planning Class 1" classnum = "41"> <student stunum = "20111111" stuname = "" stusex = ""male" stuage = "20"/> <student stunum = "20111112" stuname = "" stusex = "male" stuage = "21"/> <student stunum = "20111113" stuname = "" stusex = "female" stuage = "20"/> <student stunum = "20111114" stuname = "" stusex = "male" stuage = "19"/> </class> <class classname = "network engineer Class 1" classnum = "49"> <student stunum = "20112111" stuname = "" stusex = "male" stuage = "21" /> <student stunum = "20112112" stuname = "" stusex = "male" stuage = "20"/> <student stunum = "20112113" stuname = "" stusex = "female "stuage =" 22 "/> </class> </College>



This article is from the blog "Sanji" and will not be reproduced!

Dom parsing XML document

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.