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