How to use JSP's dom4j to read parse XML files

Source: Internet
Author: User

How to use the dom4j of the JSP tutorial to read parse XML files

<?xml version= "1.0" encoding= "GB2312"?>
<RESULT>
<VALUE>
<NO>111cn.net</NO>
<ADDR> China web first stop </ADDR>
</VALUE>

</RESULT>
<%@ page contenttype= "text/html; charset=gb2312 "language=" java "import=" java.sql.* "errorpage=" "%>"
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Untitled Document </title>

<body>
<%
Use dom4j to read XML files.
dom4j

Package test.xml;

Import Java.io.File;
Import Java.util.Iterator;
Import org.dom4j.Document;
Import org.dom4j.Element;
Import Org.dom4j.io.SAXReader;

public class Testxmlbydom4j {
public static void Main (string[] args) {
Long lasting = System.currenttimemillis ();
System.out.println ("Read by dom4j");
try {
File F = new file ("Text.xml");
Parser
Saxreader reader = new Saxreader ();
Reading and parsing files
Document doc = Reader.read (f);
Root node
Element root = Doc.getrootelement ();
Element foo;
Child node Traversal
for (Iterator i = root.elementiterator ("VALUE"); I.hasnext ();) {
Foo = (Element) i.next ();
System.out.print ("License plate number:" + foo.elementtext ("NO"));
System.out.println ("Owner address:" + foo.elementtext ("ADDR"));
}
catch (Exception e) {
E.printstacktrace ();
}
System.out.println ("Run Time:" + (System.currenttimemillis ()-lasting) + "MS");
}
}
%>
</body>

Related Article

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.