Java simple example of reading xml files

Source: Internet
Author: User

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 );}

}
}

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.