BlogJava 首頁 新隨筆 聯系 聚合 管理
            37 Posts :: 64 Stories :: 21 Comments :: 0 Trackbacks
          1.接口內容如下:
            <?xml version="1.0" encoding="UTF-8"?>
            <users>
            <user>
            <id>1</id>
            <firstname>Song</firstname>
            <lastname>Thinking</lastname>
            <password>songlipeng</password>
            </user>
            <user>
            <id>2</id>
            <firstname>Zheng</firstname>
            <lastname>Quanling</lastname>
            <password>zhengquanling</password>
            </user>
            </users>
            2.解析的JSP代碼如下:
            <%@ page language="java" import="java.util.*,javax.xml.parsers.*,java.sql.*" pageEncoding="UTF-8"%>
            <%@ page import="java.io.*,javax.xml.*,org.w3c.dom.*" %>
            <%
            //建立解析工廠
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setIgnoringElementContentWhitespace(true); //忽略元素內容中的空格
            //創建解析器
            DocumentBuilder db = dbf.newDocumentBuilder();
            //得到解析文件
            //據說這樣寫可以讓XML與JSP放在同一目錄哦
            Document doc = db.parse("http://localhost:8080/MyProjects/webserver/users.xml"); //得到其他地方的接口目錄
            doc.normalize();
            //得到根元素
            //Element root = doc.getDocumentElement();
            //得到所有user元素
            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>
            <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><%=usersPasswordNodeList.item(i).getFirstChild().getNodeValue()%></td>
            <td><%=usersTrueNameNodeList.item(i).getFirstChild().getNodeValue()%></td>
            <%
            }
            %>
            </tr>
            </table>
            <!--只提取XML中的ID數據信息-->
            <div>
            <%
            for(int i=0;i<usersIdNodeList.getLength();i++){
            out.println("<b>"+usersIdNodeList.item(i).getFirstChild().getNodeValue()+"</b>");
            }
            %>
            </div>
          posted on 2009-09-02 13:23 xiachang88 閱讀(138) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 利津县| 肥乡县| 绍兴县| 秦皇岛市| 临海市| 桂阳县| 溧水县| 正阳县| 昆明市| 察哈| 兰考县| 宜良县| 新巴尔虎左旗| 合水县| 桃园县| 延吉市| 石城县| 兖州市| 宿迁市| 铜鼓县| 东乌珠穆沁旗| 宝清县| 广水市| 宁阳县| 竹溪县| 常山县| 黎城县| 彭阳县| 柞水县| 永修县| 阿荣旗| 慈利县| 龙川县| 西乌珠穆沁旗| 韩城市| 广德县| 潮州市| 读书| 东丰县| 太保市| 朔州市|