Reprinted from: http://www.jb51.net/article/44338.htm
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;
Absolute road strength can be used
File F = new file ("Test.xml");
Documentbuilder cannot be instantiated directly (convert XML file to DOM file)
Documentbuilder db = null;
Documentbuilderfactory dbf = null;
try {
Returns the Documentbuilderfactory object
DBF = Documentbuilderfactory.newinstance ();
Returns a DB object using the Documentbuilderfatory object to get the returned Documentbuildr 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 ());
Get the child nodes under the root element
NodeList childNodes = Element.getchildnodes ();
Traverse these child nodes
for (int i = 0; i < childnodes.getlength (); i++) {
Get the node for 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 found an account. Owning area: "+ node1.getattributes (). getNamedItem (" type "). Getnodevalue () +". ");
Get the node under <Accounts>
NodeList nodedetail = Node1.getchildnodes ();
Traverse <Accounts> under a node
for (int j = 0; J < Nodedetail.getlength (); j + +) {
Get <Accounts> elements of 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 project's root directory, the content is:
<?xml version= "1.0" encoding= "GBK"?>
<Accounts>
<account type= "Type1" >
<code>100001</code>
<pass>123</pass>
<name> John Doe </name>
<money>1000000.00</money>
</Account>
<account type= "Type2" >
<code>100002</code>
<pass>123</pass>
<name> Zhang San </name>
<money>1000.00</money>
</Account>
</Accounts>
Run the code directly, output:
root element: Accounts
Find an account. Owning area: type1.
Card number: 100001
Password: 123
Name: John Doe
Balance: 1000000.00
Find an account. Owning area: type2.
Card number: 100002
Password: 123
Name: Zhang San
Balance: 1000.00
Java how to read XML file specific implementation