Tutorial on DOM parsing XML instance for reading xml class libraries from jsp

Source: Internet
Author: User

Jsp tutorial reading XML class library DOM parsing xml instance tutorial

Xml file:

<? Xml version = "1.0" encoding = "GB2312"?>
<RESULT>
<VALUE>
<NO> A1234 </NO>
<ADDR> No. XX, section X, XX Road, XX Town, XX County, Sichuan Province </ADDR>
</VALUE>
<VALUE>
<NO> B1234 </NO>
<ADDR> XX group, XX village, XX Township, xxx City, Sichuan Province </ADDR>
</VALUE>
</RESULT>

 

Test Center

Package test. xml;

/**
* Test four types of class libraries that read XML. DOM, SAX, JDOM, DOM4J <br>
* Note that the speed is not good because the first one suffers a lot.
*
*/
Public class TestXML {
Public static void main (String [] args ){
// DOM Parsing
TestXMLByDOM. main (null );
// SAX Parsing
TestXMLBySAX. main (null );
// JDOM Parsing
TestXMLByJDOM. main (null );
// DOM4J Parsing
TestXMLByDOM4J. main (null );
}
}

 


DOM

Package test. xml;

Import java. io. File;
Import javax. xml. parsers. DocumentBuilder;
Import javax. xml. parsers. DocumentBuilderFactory;
Import org. w3c. dom. Document;
Import org. w3c. dom. NodeList;

/**
* Use DOM to read XML files.
*

*/
Public class TestXMLByDOM {
Public static void main (String [] args ){
Long lasting = System. currentTimeMillis ();
System. out. println ("Read By DOM ");
Try {
// Xml file
File f = new File ("text. xml ");
// Construct the dom
DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance ();
DocumentBuilder builder = factory. newDocumentBuilder ();
// Parse the file
Document doc = builder. parse (f );
// Directly read the VALUE of the node
NodeList nl = doc. getElementsByTagName ("VALUE ");
For (int I = 0; I <nl. getLength (); I ++ ){
System. out. print ("license plate number :"
+ Doc. getElementsByTagName ("NO"). item (I). getFirstChild (). getNodeValue ());
System. out. println ("owner address :"
+ Doc. getElementsByTagName ("ADDR"). item (I). getFirstChild (). getNodeValue ());
}
} Catch (Exception e ){
E. printStackTrace ();
}
System. out. println ("Run time:" + (System. currentTimeMillis ()-lasting) + "millisecond ");
}
}

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.