Read XML resource files

Source: Internet
Author: User
During project creation, many parameters need to be modified without modifying the program to implement some parameter changes. The first thing we think of is to read the XML file. Therefore, you only need to change the parameter values in the XML file of the project. Here, I will give a brief description as follows:

Try {
Documentbuilderfactory factory = documentbuilderfactory
. Newinstance (); // getDocumentBuilderFactory(Define the factory so that the application can get the parser that generates the DOM object tree from the XML document ).
Documentbuilder builder = factory. newdocumentbuilder (); // obtain the DOM document instance from the XML document.
Java. Io. File F = new file ("file path"); // specify a file
Document document = builder. parse (f); // The interface indicates the entire HTML or XML document.
Element rootelement = Document. getdocumentelement (); // This is a convenient attribute that allows you to directly access the child nodes of the document element.

Nodelist list = rootelement. getelementsbytagname ("field"); // returns all descendants with the given tag name in document orderElementsOfNodeList
Element element = (element) List. Item (0); // extractElement
Nodelist yearlist = element. getelementsbytagname ("Cols"); // obtainGiven a tag named "Cols" under ElementNodeList

Text text = (text) yearlist. Item (0). getfirstchild (); // getNodeListFirst subnode
Nodelist yearlist1 = element. getelementsbytagname ("rows"); "); // get thisGiven in element with the name "rows"NodeList
Text text1 = (text) yearlist1.item (0). getfirstchild (); // getNodeListFirst subnode
Hang = integer. parseint (text. getdata (); // get the node Value
Lie = integer. parseint (text1.getdata ());
} Catch (exception e ){
System. Out. println ("exception:" + E. getmessage ());
}
My XML documents are as follows:
<Hxw>
<Field ID = "1">
<Cols> 5 </Cols>
<Rows> 10 </rows>
</Field>
</Hxw>
Of course, if there are more than one field, it can be read through the for loop.
Now let's talk about it here. You are welcome to give your valuable comments :)

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.