Java Read XML

Source: Internet
Author: User

The method is not many, can use is good.

I'm using dom4j.

    <Dependency>     <groupId>dom4j</groupId>     <Artifactid>dom4j</Artifactid>     <version>1.6.1</version>    </Dependency>

The contents of the read file are as follows:

<?XML version= "1.0" encoding= "UTF-8"?><Users>    <ModuleID= "1">        <UserIndex= "1">            <name>Tom</name>            <Password>12345</Password>            <Date>20150526</Date>        </User>        <UserIndex= "2">            <name>Jack</name>            <Password>5%</Password>            <Date>20150526</Date>        </User>        <UserIndex= "3">            <name>John</name>            <Password>5%</Password>            <Date>20150526</Date>        </User>    </Module></Users>

The reading idea is:

1. Create a Saxreader instance;

2. Create a file to read the BufferedReader instance;

3. Create a document instance to read BufferedReader;

4. Get the root node of the XML file;

5. Gets the child nodes of the root node;

6. Traverse the child node, get the node name with GetName (), get the value of the node with GetText (), get the property value with AttributeValue (String)

The code to get the root node is as follows:

     Public StaticList<element>readXml (String FilePath) {BufferedReader in=NULL; List<Element> elementlist =NULL; Document Doc=NULL; Saxreader Reader=NewSaxreader (); Try{ in=NewBufferedReader (NewFileReader (FilePath)); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        Try{doc=Reader.read (in); } Catch(documentexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Element Root=doc.getrootelement (); Elementlist=root.elements (); returnelementlist; }

To traverse the child nodes, read the code for the user name and password as follows:

@SuppressWarnings ("Unchecked")     PublicListreaduserdotxml (string path, String module_id) {ListNewArraylist(); String RootPath=path; List<Element> list =Readxml.readxml (RootPath); if(List! =NULL) {             for(Element ele:list) {String index= Ele.attributevalue ("id"); if(module_id.equals (index)) {List<Element> userlist =ele.elements (); if(UserList! =NULL&& userlist.size () >0){                         for(Element user:userlist) {HashMap<String,String> HashMap =NewHashmap<string, string>(); Element name= User.element ("name"); Element Password= User.element ("Password"); String Namevalue=Name.gettext (); String Passwordvalue=Password.gettext (); Hashmap.put ("Name", Namevalue); Hashmap.put ("Password", Passwordvalue);                        Users.add (HASHMAP); }                    }                                         Break; }            }        }                returnusers; }

The main function call method is as follows:

List);  for (hashmap<string, string> hashmap:resultlist) {    System.out.println (hashmap.get ("name"));    System.out.println (Hashmap.get ("password"));}

Using Java total feel without Python so simply, here a few more steps, there are more steps. The next time you summarize the XML read for Python

Java Read XML

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.