以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XML源码及示例(仅原创和转载) 』  (http://bbs.xml.org.cn/list.asp?boardid=32)
----  如何将XML文件中的数据传送并保存在关系数据库中(1)[转帖]  (http://bbs.xml.org.cn/dispbbs.asp?boardid=32&rootid=&id=44056)


--  作者:wuyou125
--  发布时间:3/18/2007 1:42:00 PM

--  如何将XML文件中的数据传送并保存在关系数据库中(1)[转帖]
【导读】在AJAX、网络服务与纯XML之间存在大量的数据传输。XML确实使数据传输更加方便。虽然这相当不错,不过它完全忽略了一个事实,即最终数据必须存储在某个地方,最可能是在一个关系数据库中。这带来了一个问题:如何将XML文件中的信息存储到关系数据库中呢?

在AJAX、网络服务与纯XML之间存在大量的数据传输。XML确实使数据传输更加方便。虽然这相当不错,不过它完全忽略了一个事实,即最终数据必须存储在某个地方,最可能是在一个关系数据库中。这带来了一个问题:如何将XML文件中的信息存储到关系数据库中呢?

理想情况下,这种程序很明显;但事实并非如此。哎!如果我长得像布拉德皮特并拥有比尔盖茨的支票薄就好了。我使它接近完美,我做出如下选择:

整容手术
释放我的机器人杀手军队
哈,错误的选择。再试一次。

每次插入使用一个单独SQL语句的单纯循环方法。
建立许多可以立即执行的界定SQL语句的单纯循环方法。
应用XSL建立SQL的科学方法。
我会选择哪个方法,介意猜一猜吗?

对,我肯定会选择第三个方法。所以,让我们了解一下我们将要处理的XML,如列表A所示。没有华而不实,只是必要的概念证据。

列表A——输入XML文件

<?xml version="1.0" ?>
<!-- Edited with the Butterfly XML Editor (http://www.butterflyxml.org) -->
<root>
<row>
<state_id>PA</state_id>
<state_name>Pennsylvania</state_name>
</row>
<row>
<state_id>NJ</state_id>
<state_name>New Jersey</state_name>
</row>
</root>

下面,我们查看一下我们建立插入的表格,如表A:

表A——Tarqet表

state_id
VARCHAR2(2)

state_name
VARCHAR2(50)

利用这些信息,我们可以采取两种可能的行动。第一种是建立一个XSL式样表,它模仿第一个方法:“每次插入使用一个单独SQL语句的单纯循环方法”。这个方法具有速度与通用性的优势,毕竟XSL是一个世界通用的标准。

如列表B所示,这个任务所需的XSL与多数其它XSL类似,只有少数几点不同,下面我来分别指出这些差异:首先,专门针对介质类型的xsl:output元素。在这个式样表中,介质类型设定为text/sql,而非默认的text/xml或常见的text/html。另外一个巨大的差异就是其中包含了in.xsl和sqlApostrophe.xsl,它们用来决定那些元素中与数字相对的文本或日期,并用双引号代替单引号来防止SQL问题。另外,还有用来建立输出的xsl:text和xsl:value-of元素。


列表B——XSL式样表

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="table"/>
<xsl:param name="textColumns"/>
<xsl:param name="dateColumns"/>
<xsl:includehref="in.xsl"/>
<xsl:includehref="sqlApostrophe.xsl"/>
<!--
Stylesheet:sample.xsl
Creation Date:October 25, 2006
Programmer:Edmond Woychowsky
Purpose:The purpose of this XSL style sheet isto generate multiple SQL insert statements.
Template:match="/"
Creation Date:October 25, 2006
Programmer:Edmond Woychowsky
Purpose:The purpose of this template is to create the outer sql element and invoke the template forthe individual INSERT statements.
Update Date:Programmer:Description:
-->
<xsl:template match="/">
<xsl:element name="sql">
<xsl:apply-templates select="//row"/>
</xsl:element>
</xsl:template>
<!--
Template:match="row"
Programmer:Edmond Woychowsky
Purpose:The purpose of this template is to control the creation of the INSERT statements.
Update Date:Programmer:Description:
-->
<xsl:template match="row">
<xsl:element name="statement">
<xsl:value-of select="concat('INSERT INTO ',$table,' (')"/>
<xsl:apply-templates select="*" mode="name"/>
<xsl:text>) VALUES (</xsl:text>
<xsl:apply-templates select="*" mode="value"/>
<xsl:text>)</xsl:text>
</xsl:element>
</xsl:template>
<!--
Template:match="*" mode="name"
Programmer:Edmond Woychowsky
Purpose:The purpose of this template is to list the column names.
Update Date:Programmer:Description:
-->
<xsl:template match="*" mode="name">
<xsl:if test="position() != 1">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:value-of select="name(.)"/>
</xsl:template>
<!--
Template:match="*" mode="value"
Programmer:Edmond Woychowsky
Purpose:The purpose of this template is to list the column values.
Update Date:Programmer:Description:
-->
<xsl:template match="*" mode="value">
<xsl:variable name="text">
<xsl:call-template name="in">
<xsl:with-param name="list" select="$textColumns"/>
<xsl:with-param name="value" select="name(.)"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="date">
<xsl:call-template name="in">
<xsl:with-param name="list" select="$dateColumns"/>
<xsl:with-param name="value" select="name(.)"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="position() != 1">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:choose>
<xsl:when test="$text = 'true'">
<xsl:text>'</xsl:text>
<xsl:call-template name="sqlApostrophe">
<xsl:with-param name="string" select="."/>
</xsl:call-template>
<xsl:text>'</xsl:text>
</xsl:when>
<xsl:when test="$date = 'true'">
<xsl:value-of select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

当然,这个方法仍然存在问题,我们必须遍历XML文件来逐个执行SQL语句,这是一件我最不喜欢的事情。但是,这个问题有别的解决方法:将单独的语句用逗号分隔开来,建立一个复合SQL语句。


我不认为我懒惰,相反我认为自己有效率。毕竟这个方法比前一个方法更不易出错。因此只需稍稍做一些改变,主要是转移一些代码,我建立了如列表C所示的XSL式样表。

列表C——高效XSL样式表

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="table"/>
<xsl:param name="textColumns"/>
<xsl:param name="dateColumns"/>
<xsl:includehref="in.xsl"/>
<xsl:includehref="sqlApostrophe.xsl"/>
<!--
Stylesheet:sample.xsl
Creation Date:October 25, 2006
Programmer:Edmond Woychowsky
Purpose:The purpose of this XSL style sheet is to generate multiple SQL insert statements.
Template:match="/"
Creation Date:October 25, 2006
Programmer:Edmond Woychowsky
Purpose:The purpose of this template is to create the outer sql element and invoke the template for the individual INSERT statements.
Update Date:Programmer:Description:
-->
<xsl:template match="/">
<xsl:element name="sql">
<xsl:apply-templates select="//row"/>
</xsl:element>
</xsl:template>
<!--
Template:match="row"
Programmer:Edmond Woychowsky
Purpose:The purpose of this template isto control the creation of the INSERT statements.
Update Date:Programmer:Description:
-->
<xsl:template match="row">
<xsl:value-of select="concat('INSERT INTO ',$table,' (')"/>
<xsl:apply-templates select="*" mode="name"/>
<xsl:text>) VALUES (</xsl:text>
<xsl:apply-templates select="*" mode="value"/>
<xsl:text>)</xsl:text>
</xsl:template>
<!--
Template:match="*" mode="name"
Programmer:Edmond Woychowsky
Purpose:The purpose of this template is to list the column names.
Update Date:Programmer:Description:
-->
<xsl:template match="*" mode="name">
<xsl:if test="position() != 1">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:value-of select="name(.)"/>
</xsl:template>
<!--
Template:match="*" mode="value"
Programmer:Edmond Woychowsky
Purpose:The purpose of this template is to list the column values.
Update Date:Programmer:Description:
-->
<xsl:template match="*" mode="value">
<xsl:variable name="text">
<xsl:call-template name="in">
<xsl:with-param name="list" select="$textColumns"/>
<xsl:with-param name="value" select="name(.)"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="date">
<xsl:call-template name="in">
<xsl:with-param name="list" select="$dateColumns"/>
<xsl:with-param name="value" select="name(.)"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="position() != 1">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:choose>
<xsl:when test="$text = 'true'">
<xsl:text>'</xsl:text>
<xsl:call-template name="sqlApostrophe">
<xsl:with-param name="string" select="."/>
</xsl:call-template>
<xsl:text>'</xsl:text>
</xsl:when>
<xsl:when test="$date = 'true'">
<xsl:value-of select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

结论

过去几年来,我一直在使用这种,或与它类似的方法,都取得了很好的效果。尽管从SQL角度看,它的执行速度比不上比更常见的建立SQL的字符串串联方法提供更好结果的存储过程。我还想指出的是,我说过我使用这个或与它类似的方法,基本只增加了commit和rollback语句来处理错误。不过,这些修改仅仅是个人尝试和个人偏执行为。


--  作者:cdhyy
--  发布时间:3/30/2007 10:51:00 AM

--  
斑竹能推荐本相关的书吗?
--  作者:emmali808
--  发布时间:3/12/2008 3:06:00 PM

--  
参考参考!
--  作者:LYF100
--  发布时间:4/4/2008 8:52:00 PM

--  
bucuo
--  作者:hongxingby
--  发布时间:4/10/2008 2:58:00 PM

--  
学习中……
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
62.500ms