JSP parsing XML document using JDOM read XML file

Source: Internet
Author: User
Tags gettext

JSP tutorial parsing XML documents using JDOM reading XML files
XML file:

<?xml version= "1.0" encoding= "GB2312"?>
<RESULT>
<VALUE>
<NO>A1234</NO>
<ADDR> xx, xx Road, xx County, Sichuan Province, x section xx </ADDR>
</VALUE>
<VALUE>
<NO>B1234</NO>
<ADDR> xx xiang xx cun xx zu, xx City, Sichuan province </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>
<%
Jdom

Package test.xml;

Import Java.io.File;
Import java.util.List;
Import org.jdom.Document;
Import org.jdom.Element;
Import Org.jdom.input.SAXBuilder;

/**
* Read XML files using JDOM.
*

*/
public class Testxmlbyjdom {
public static void Main (string[] args) {
Long lasting = System.currenttimemillis ();
System.out.println ("Read by JDOM");
try {
Structure
Saxbuilder builder = new Saxbuilder ();
Reading documents
Document doc = builder.build (new File ("Text.xml"));
Get root
Element foo = doc.getrootelement ();
Get subordinate Node
List Allchildren = Foo.getchildren ();
for (int i = 0; i < allchildren.size (); i++) {
System.out.print ("License plate Number:" + (Element) allchildren.get (i)). Getchild ("NO"). GetText ());
System.out.println ("Owner's Address:" + ((Element) allchildren.get (i)). Getchild ("ADDR"). GetText ());
}
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.