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!