XML data parsing interface for JSP

Source: Internet
Author: User

1. The interface content is as follows:

<?xml version= "1.0" encoding= "UTF-8"?>

            <users>

            <user>

            <id>1</id>
  
    <firstname>Song</firstname>

            <lastname>Thinking</lastname>

            <pas        sword>songlipeng</password>

            </user>

            <user>

            <id>2</id>

            <firstname>Zheng</firstname>

            <lastname>Quanling</lastname>

            <pas sword>zhengquanling</password>

            </user>

            </users>
  

2. The parsed JSP code is as follows:

<%@ page language= "java" import= "java.util.*,javax.xml.parsers.*,java.sql.*" pageencoding= "UTF-8"%> &L

                t;%@ page import= "java.io.*,javax.xml.*,org.w3c.dom.*"%> <%//Set up resolution factory

                Documentbuilderfactory dbf = Documentbuilderfactory.newinstance ();      Dbf.setignoringelementcontentwhitespace (TRUE);

                Ignore spaces in element content//create parser Documentbuilder db = Dbf.newdocumentbuilder (); Get resolution File//It is said that this write can let XML and JSP in the same directory Oh doc = Db.parse ("Http://localhost:8080/MyProje     Cts/webserver/users.xml ");

                Get the interface directory of other places doc.normalize ();

                Gets the root element//element root = Doc.getdocumentelement ();

                Get all user elements nodelist users = doc.getelementsbytagname ("user");

                NodeList usersidnodelist= doc.getelementsbytagname ("id"); NodeList Usersnamenodelist= doc.getelementsbytagname ("FirstName");

                NodeList Userspasswordnodelist=doc.getelementsbytagname ("LastName");

            NodeList userstruenamenodelist=doc.getelementsbytagname ("password"); %> <table> <thead> <tr> & Lt;th>id</th> <th>firstName</th> <th>lastname </th> <th>password</th> </tr> </

                    thead> <% Node usernode = null;

                for (int i = 0; i < users.getlength (); i++) {//element user = (Element) users.item (i); %> <tr> <td><%=usersidnodelist.item (i) getfirstchild () . Getnodevalue ()%></td> <td><%=usersnamenodelist.item (i). Getfirstchild (). Getnodevalue ()%></td> <td><%=userspass Wordnodelist.item (i). Getfirstchild (). Getnodevalue ()%></td> <td><%=userstruenamenode

                    List.item (i). Getfirstchild (). Getnodevalue ()%></td> <%}

            %> </tr> </table> <!--extract only the ID data information in the XML-->

                        <center> <% for (int i=0;i<usersidnodelist.getlength (); i++) {

                     Out.println ("<b>" +usersidnodelist.item (i). Getfirstchild (). Getnodevalue () + "</b>"); }%> </center>

So the XML words can be so other places are Java to write data and then I parse it out here!

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.