Java how to read XML file implementation _java

Source: Internet
Author: User

Today's Csdn FAQ explains how to read the contents of an XML file in Java.

Directly on the code, the note is very clear!

Copy Code code as follows:

Import java.io.*;
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 Xmlreadertest {

public static void Main (String args[]) {
element element = null;
can use absolute road strength
File F = new file ("Test.xml");

Documentbuilder cannot be directly instantiated for abstraction (convert XML file to DOM file)
Documentbuilder db = null;
Documentbuilderfactory dbf = null;
try {
Returns the Documentbuilderfactory object
DBF = Documentbuilderfactory.newinstance ();
Returns the DB object to get the returned Documentbuildr object with the Documentbuilderfatory object
db = Dbf.newdocumentbuilder ();

Get a DOM and return it to the Document object
Document dt = Db.parse (f);
Get a elment root element
element = Dt.getdocumentelement ();
Get root node
SYSTEM.OUT.PRINTLN ("root element:" + element.getnodename ());

To get the child nodes under the root element
NodeList childnodes = Element.getchildnodes ();

Traversing these child nodes
for (int i = 0; i < childnodes.getlength (); i++) {
Get the node of each corresponding position I
Node Node1 = Childnodes.item (i);
if ("Account". Equals (Node1.getnodename ()) {
If the name of the node is account, the account element property type is output
System.out.println ("\ r \ n Find an account.") Area: "+ node1.getattributes (). getNamedItem (" type "). Getnodevalue () +". ");
Get the node under <Accounts>
NodeList Nodedetail = Node1.getchildnodes ();
Traversal <Accounts> the nodes below
for (int j = 0; J < Nodedetail.getlength (); j + +) {
Get <Accounts> Element each node
Node detail = Nodedetail.item (j);
if ("Code". Equals (Detail.getnodename ())/output code
SYSTEM.OUT.PRINTLN ("card number:" + detail.gettextcontent ());
else if ("Pass". Equals (Detail.getnodename ())/output Pass
System.out.println ("Password:" + detail.gettextcontent ());
else if ("name". Equals (Detail.getnodename ())/output name
System.out.println ("Name:" + detail.gettextcontent ());
else if ("Money". Equals (Detail.getnodename ())/output money
System.out.println ("Balance:" + detail.gettextcontent ());
}
}

}
}

catch (Exception e) {
E.printstacktrace ();
}
}
}

Then we test the XML file (Test.xml) to be placed in the root directory of the project project, and its contents are:

Copy Code code as follows:

<?xml version= "1.0" encoding= "GBK"?>
<Accounts>
<account type= "Type1" >
<code>100001</code>
<pass>123</pass>
<name> Dick </name>
<money>1000000.00</money>
</Account>
<account type= "Type2" >
<code>100002</code>
<pass>123</pass>
<name> John </name>
<money>1000.00</money>
</Account>
</Accounts>

Run the code directly, output:

root element: Accounts

Find an account. Area: Type1.
Card number: 100001
Password: 123
Name: Dick
Balance: 1000000.00

Find an account. Area: type2.
Card number: 100002
Password: 123
Name: John
Balance: 1000.00

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.