amazon.jsp
上传用户:shjgzm
上传日期:2017-08-31
资源大小:2757k
文件大小:5k
源码类别:

Ajax

开发平台:

Java

  1. <%@ page contentType="text/xml; charset=UTF-8"%>
  2. <%@ page language="java"%>
  3. <%@ page import="java.util.*,java.io.*,org.dom4j.*,org.dom4j.io.*"%>
  4. <%@ page import="java.sql.*,ajax.db.DBUtils,ajax.util.XMLUtils"%>
  5. <%
  6.     out.clear();                                    //清空当前的输出内容(空格和换行符)
  7.     String browseNodeId = request.getParameter("browseNodeId");//获取要浏览的browseNodeId参数
  8.     //创建用于保存xmlTree信息的StringBuffer对象
  9.     StringBuffer xmlTree= new StringBuffer("<?xml version="1.0" encoding="UTF-8"?>");
  10.     xmlTree.append("<tree>");                           //xmlTree根节点为<tree>
  11.     //如果获取的是第一层目录,从数据库中读取并生成
  12.     if ("0".equals(browseNodeId)) {
  13.         String sql = "select * from amazon_categories order by seq asc";//定义查询数据库的SQL语句
  14.         Connection conn = null;                 //声明Connection对象
  15.         PreparedStatement pstmt = null;         //声明PreparedStatement对象
  16.         ResultSet rs = null;                    //声明ResultSet对象
  17.         try {
  18.             conn = DBUtils.getConnection();     //获取数据库连接
  19.             pstmt = conn.prepareStatement(sql); //根据sql创建PreparedStatement
  20.             rs = pstmt.executeQuery();          //执行查询,返回结果集
  21.             while (rs.next()) {                 //遍历结果集创建item节点
  22.                 xmlTree.append("<item id="");
  23.                 xmlTree.append(rs.getString("id"));
  24.                 xmlTree.append("" isFolder="");
  25.                 xmlTree.append(true);
  26.                 xmlTree.append("">");
  27.                 xmlTree.append(XMLUtils.escapeXML(rs.getString("name")));
  28.                 xmlTree.append("</item>");
  29.             }
  30.         } catch (SQLException e) {
  31.             System.out.println(e.toString());
  32.         } finally {
  33.             DBUtils.close(rs);         //关闭结果集
  34.             DBUtils.close(pstmt);      //关闭PreparedStatement
  35.             DBUtils.close(conn);       //关闭连接
  36.         }
  37.     //否则使用DOM4j从远程读取信息
  38.     } else {
  39.         String myAccessKeyId = "0WTFYB410YD3GXRYR602";  //从Amazon申请的访问keyId
  40.         //访问的目标URL
  41.         String url = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=" + myAccessKeyId + "&Operation=BrowseNodeLookup&BrowseNodeId=" + browseNodeId;
  42.         SAXReader reader = new SAXReader();     //创建一个SAXReader对象
  43.         Document doc = reader.read(url);        //从远程读取并解析XML
  44.         //设置访问BrowseNodeId属性的XPath
  45.         XPath xpath1 = doc.createXPath("//am:Children/am:BrowseNode/am:BrowseNodeId");
  46.         xpath1.setNamespaceURIs(Collections.singletonMap("am", "http://webservices.amazon.com/AWSECommerceService/2005-10-05"));
  47.         //设置访问Name属性的XPath
  48.         XPath xpath2 = doc.createXPath("//am:Children/am:BrowseNode/am:Name");
  49.         xpath2.setNamespaceURIs(Collections.singletonMap("am", "http://webservices.amazon.com/AWSECommerceService/2005-10-05"));
  50.         List idNodes = xpath1.selectNodes(doc);     //使用XPath获取BrowseNodeId节点列表
  51.         List nameNodes = xpath2.selectNodes(doc);   //使用XPath获取Name节点列表
  52.         int nodeSize = idNodes.size();              //获取列表长度
  53.         //如果列表长度为0,表示当前请求的节点为叶子节点
  54.         if (nodeSize == 0) {
  55.             //设置读取叶子节点编号的XPath
  56.             xpath1 = doc.createXPath("//am:BrowseNodes/am:BrowseNode/am:BrowseNodeId");
  57.             xpath1.setNamespaceURIs(Collections.singletonMap("am", "http://webservices.amazon.com/AWSECommerceService/2005-10-05"));
  58.             //设置读取叶子节点名称的XPath
  59.             xpath2 = doc.createXPath("//am:BrowseNodes/am:BrowseNode/am:Name");
  60.             xpath2.setNamespaceURIs(Collections.singletonMap("am", "http://webservices.amazon.com/AWSECommerceService/2005-10-05"));
  61.             //将叶子节点写入xmlTree
  62.             xmlTree.append("<item id="");
  63.             xmlTree.append(xpath1.selectSingleNode(doc).getText());
  64.             xmlTree.append("" isFolder="");
  65.             xmlTree.append("false");                //注意设置isFolder为false
  66.             xmlTree.append("">");
  67.             xmlTree.append(XMLUtils.escapeXML(xpath2.selectSingleNode(doc).getText()));
  68.             xmlTree.append("</item>");
  69.         //否则请求节点为目录节点,需要遍历读取其信息
  70.         } else {
  71.             //遍历节点列表,读取节点信息,写入xmlTree
  72.             for (int i=0; i<nodeSize; i++) {
  73.                 Node idNode = (Node) idNodes.get(i);
  74.                 Node nameNode = (Node) nameNodes.get(i);
  75.                 xmlTree.append("<item id="");
  76.                 xmlTree.append(idNode.getText());
  77.                 xmlTree.append("" isFolder="");
  78.                 xmlTree.append("true");
  79.                 xmlTree.append("">");
  80.                 xmlTree.append(XMLUtils.escapeXML(nameNode.getText()));
  81.                 xmlTree.append("</item>");
  82.             }
  83.         }
  84.     }
  85.     xmlTree.append("</tree>");     //xmlTree根节点的结束标签
  86.     out.print(xmlTree.toString()); //输出xmlTree
  87. %>