如何从DB2 XML列生成HTML表?

时间:2015-02-09 14:46:15

标签: xml xsd db2 html-table

我在DB2中生成了这个表:

CREATE TABLE "MGMT"."SYSTEM" 
(   "SYSTEM_ID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY ( START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 NO CYCLE CACHE 20),
        "NAME" VARCHAR(300) NOT NULL,
        "SERVICE" VARCHAR(200) NOT NULL,
        "VERSION" VARCHAR(150) NOT NULL,
        "CONFIGURATION" XML NOT NULL
    )

然后我将这个XML文档存储在"配置"柱:

<?xml version="1.0" encoding="UTF-8"?>
<system xmlns:xi="http://www.w3.org/2001/XInclude" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:noNamespaceSchemaLocation="SystemConfiguration.xsd" 
        xsi:schemaLocation="http://www.w3.org/XML/1998/namespace ../xinclude/xml.xsd http://www.w3.org/2001/XInclude        ../xinclude/xinclude.xsd ">
<components>
    <component>
        <name>FileNet</name>
        <version>5.2.1.0</version>
        <buildNumber>dap521.000.024</buildNumber>
    </component>
    <component>
        <name>CSS</name>
        <version>IQQD0055E The search server is stopped. It must be started for the tool to run.</version>
    </component>
    <component>
        <name>CSS</name>
        <version>2.1.0.0-1.1-4147.</version>
        <fixpack>FP5</fixpack>
    </component>
    <component>
        <name>TDS</name>
        <version>6.3.1.0</version>
    </component>
    <component>
        <name>ICN</name>
        <version>2.0.2</version>
    </component>
    <component>
        <name>CMIS</name>
        <version>IBM Content Management Interoperability Services for FileNet Content Manager (IBM CMIS) Version 2.0.2.3</version>
    </component>
    <component>
        <name>DB2</name>
        <version>DB2 v10.1.0.2</version>
    </component>
    <component>
        <name>WAS</name>
        <version>8.5.5.1</version>
    </component>
</components>
</system>

最后,我定义了一个与上述XML相对应的XML Schema Document(XSD):

<?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="system">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element minOccurs="0" ref="config"/>
        <xsd:element ref="components"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="config">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element maxOccurs="unbounded" ref="parameter"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="components">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element maxOccurs="unbounded" ref="component"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="component">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="name"/>
        <xsd:element ref="version"/>
        <xsd:element minOccurs="0" ref="fixpack"/>
        <xsd:element minOccurs="0" ref="buildNumber"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="parameter">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="name"/>
        <xsd:element ref="version"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="name" type="xsd:string"/>
  <xsd:element name="version" type="xsd:string"/>
  <xsd:element name="fixpack" type="xsd:string"/>
  <xsd:element name="buildNumber" type="xsd:string"/>
</xsd:schema>

我想在JSP页面中将XML呈现为HTML表格,如下所示,但我遇到了错误。

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page import="java.sql.*"%>
<%@ page import="java.lang.*"%>
<%@ page import=" javax.xml.parsers.DocumentBuilder"%>
<%@ page import=" javax.xml.parsers.DocumentBuilderFactory"%>
<%@ page import="org.xml.sax.InputSource"%>
<%@ page import="java.io.StringReader"%>
<%@ page import="org.w3c.dom.Document"%>
<%@ page import="org.xml.sax.SAXException"%>
<%@ page import="javax.servlet.http.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<%@ page import = "javax.xml.transform.*"%>
<%@ page import=" java.io.File"%>
<%@ page import="org.w3c.dom.Node"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<body>
    <%
        Class.forName("com.ibm.db2.jcc.DB2Driver");

        String url = "jdbc:db2://localhost:50000/INOCQADB";
        String user = "db2admin";
        String password = "forever2007?";

        Connection connection = null;
        PreparedStatement selectKunden = null;
        ResultSet resultSet = null;

        String id = request.getParameter("xml");

        int zahl;
        zahl = Integer.parseInt(id);

        try {

            connection = DriverManager.getConnection(url, user, password);

            selectKunden = connection
                    .prepareStatement("SELECT MGMT.System.Configuration FROM MGMT.System WHERE MGMT.System.SYSTEM_ID =?");
            selectKunden.setInt(1, zahl);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {

            resultSet = selectKunden.executeQuery();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    %>

    <table border="1">
                <%

                    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                    Document doc = null;
                    doc = docBuilder.parse(resultSet.getString("Configuration"));
                    doc.getDocumentElement().normalize();

                    Node node = doc.getDocumentElement();

                    Node componentsNode = null;

                    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
                        if (node.getChildNodes().item(i).getNodeName().equalsIgnoreCase("components")) {
                            componentsNode = node.getChildNodes().item(i);
                        }
                    }

                    for (int i = 0; i < componentsNode.getChildNodes().getLength(); i++) {
                        Node childNode = componentsNode.getChildNodes().item(i);
                        if (childNode.getNodeName().equalsIgnoreCase("component")) {


                %>
                <tr>
                    <%  if (childNode.hasChildNodes()) {
                            for (int k = 0; k < childNode.getChildNodes().getLength(); k++) {
                                Node cchildNode = componentsNode.getChildNodes().item(k);
                    %>
                    <td><%cchildNode.getNodeName();%></td>
                    <%
                        }

                    %>
                </tr>
                <%   }}
                    }

                %>
            </table>
    </body>
    </html>

我收到以下错误:

Error 500: com.ibm.db2.jcc.am.SqlException: [jcc][t4][1090][10899][4.14.113] Invalid operation to read at current cursor position. ERRORCODE=-4476, SQLSTATE=02501

我该怎么做?任何人都可以提供代码示例吗?

1 个答案:

答案 0 :(得分:0)

XMLTABLE SQL / XML函数可以将XML文档转换为传统的多行结果集,可以通过标准JDBC调用将其检索到JSP中。这个developerWorks article演示了这个过程。

以下是对您的表格和文档结构的XMLTABLE示例调用:

SELECT X.NAME, X.VERSION, X.FIXPACK, X.BUILDNUMBER 
FROM MGMT.SYSTEM,
XMLTABLE('$CONFIGURATION/system/components/component' 
       COLUMNS 
       NAME VARCHAR(30) PATH 'name',
       VERSION VARCHAR(120) PATH 'version',
       FIXPACK VARCHAR(30) PATH 'fixpack',
       BUILDNUMBER VARCHAR(20) PATH 'buildNumber'
 ) AS X
WHERE MGMT.SYSTEM.SYSTEM_ID = ?
;

对于您提供的XML文档,上面的SQL语句将返回八行,这对应于它包含的<component>个元素的数量。通过从JSP准备和执行该语句,您可以消除解析XML文档的Java代码。