精彩专题推荐:建站之入门课 建站之必修课 建站之关键课 网站价值所在 流量提高专题 css+div 标准 个人网站打造全过程
返回建站学首页
导航:
建站首页 | 网站设计 | 网站开发 | 网站运营 | 网页软件 | 建站指南 | 搜索优化 | 图像处理 | 视频教程 | 书籍教程 | 建站专题
当前位置:首页>网站开发>xml教程>正文

如何将XML文件中的数据传送并保存在关系数据库中


来源: 时间:07-02-10 点击: 点击这里收藏本文

在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.xslsqlApostrophe.xsl,它们用来决定那些元素中与数字相对的文本或日期,并用双引号代替单引号来防止SQL问题。另外,还有用来建立输出的xsl:textxsl: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 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 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 is to 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语句来处理错误。不过,这些修改仅仅是个人尝试和个人偏执行为。


  把此文章收藏到:          
广而告之
文章搜索
  • Google JZxue.Com

关于我们 | 联系我们 | 友情链接 | 网站地图
Copyright © 2005 - 2006 建站学 All rights reserved.