Xml is a special data storage format in plain text format. xml is often used for various data api interactions and data exchange before different scripts, let's take a look at a simple example of java reading xml files.
The XML file is as follows:
| The Code is as follows: |
Copy code |
|
<? Xml version = "1.0" encoding = "gbk"?>
<Accounts>
<Account type = "by0003">
<Code> 100001 </code>
<Passed> 123 </pass>
<Name> Li Si </name>
<Money> 1000000.00 </money>
</Account>
<Account type = "hz0001">
<Code> 100002 </code>
<Passed> 123 </pass>
<Name> Zhang San </name>
<Money> 1000.00 </money>
</Account>
</Accounts> |
The java xml reading class is as follows:
| The Code is as follows: |
Copy code |
|
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 xmljava
{
Public static void main (String args [])
{
Element element = null;
File f = new File ("a. xml ");
DocumentBuilder db = null; // documentBuilder cannot be directly instantiated as an abstraction (converting an XML file to a DOM file)
DocumentBuilderFactory dbf = null;
Try {
Dbf = DocumentBuilderFactory. newInstance (); // return the documentBuilderFactory object
Db = dbf. newDocumentBuilder (); // return the database object. Use documentBuilderFatory to obtain the returned documentBuildr object.
Document dt = db. parse (f); // get a DOM and return it to the document Object.
Element = dt. getDocumentElement (); // get an elment root element.
System. out. println ("root element:" + element. getNodeName (); // obtain the root node
NodeList childNodes = element. getChildNodes (); // obtain the subnode under the root element
For (int I = 0; I <childNodes. getLength (); I ++) // traverse these subnodes
{
Node node1 = childNodes. item (I); // childNodes. item (I); obtain the Node of each corresponding position I
If ("Account". equals (node1.getNodeName ()))
{
// If the node name is "Account", the type of the Account element attribute is output.
System. out. println ("rn finds an account. region :"
+ Node1.getAttributes (). getNamedItem ("type"). getNodeValue () + ".");
NodeList nodeDetail = node1.getChildNodes (); // obtain the node under <Accounts>
For (int j = 0; j <nodeDetail. getLength (); j ++)
{// Traverse the nodes under <Accounts>
Node detail = nodeDetail. item (j); // obtain each Node of the <Accounts> element
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) {System. out. println (e );}
}
} |