BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            37 Posts :: 64 Stories :: 21 Comments :: 0 Trackbacks
          1.接口內(nèi)容如下:
            <?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); //忽略元素內(nèi)容中的空格
            //創(chuàng)建解析器
            DocumentBuilder db = dbf.newDocumentBuilder();
            //得到解析文件
            //據(jù)說這樣寫可以讓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數(shù)據(jù)信息-->
            <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)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 农安县| 萍乡市| 西畴县| 霍州市| 大姚县| 新余市| 普洱| 金寨县| 临桂县| 宝鸡市| 天气| 中卫市| 九台市| 屏南县| 永兴县| 五华县| 旺苍县| 鹤庆县| 宝丰县| 修水县| 康保县| 九寨沟县| 荔波县| 无棣县| 清流县| 天镇县| 宁南县| 都安| 马尔康县| 呼和浩特市| 宁河县| 丹江口市| 张家口市| 夏河县| 社旗县| 咸宁市| 万州区| 南投市| 阿克| 邢台市| 克东县|