【问题标题】:Storing XML into Postgres将 XML 存储到 Postgres
【发布时间】:2015-06-15 13:05:54
【问题描述】:

我有一个需要存储在 SQL 数据库 (Postgres) 中的 XML 文档。 我已经看到它是如何完成的,但我有一个问题:我是否只是创建一个带有 xml 字段的表并将整个文档放在那里?这是关于电影等(电影、演员...)的文档,其中包含稍后检索的信息。

我从来没有在数据库中使用过 XML,所以我有点困惑。

这是我的 XML 示例:

<?xml version="1.0" encoding="UTF-8"?>
<cinema xmlns="movies"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="movies file:/C:/Users/Fabio/git/LAPD/movies.xsd">
<persons>
    <person id="P1">
        <name>Samuel L. Jackson</name>
        <birth>1948-12-21</birth>
    </person>
    <person id="P2">
        <name>Leonardo Di Caprio</name>
        <birth>1974-11-11</birth>
    </person>
    <person id="P3">
        <name>Quentin Tarantino</name>
        <birth>1963-03-27</birth>
    </person>
</persons>
<movies>
    <movie id="M1">
        <title>Pulp Fiction</title>
        <length>154</length>
        <year>1994</year>
        <description>The lives of two mob hit men, 
            a boxer, a gangster's wife, and a pair 
            of diner bandits intertwine in four tales of violence and redemption</description>
        <crew>
            <director ref="P3"/>
            <writer ref="P3"/>
        </crew>
        <cast>
            <actor ref="P1"/>
        </cast>
        <rate>
            <imdb>8.9</imdb>
            <rottentomatoes>9</rottentomatoes>
            <moviedb>7.8</moviedb>
            <average>8.57</average>
        </rate>
        <numOscars>1</numOscars>
    </movie>
    <movie id="M2">
        <title>Django Unchained</title>
        <length>165</length>
        <year>2012</year>
        <description>With the help of a German bounty hunter, 
            a freed slave sets out to rescue his wife 
            from a brutal Mississippi plantation owner.</description>
        <crew>
            <director ref="P3"/>
            <writer ref="P3"/>
        </crew>
        <cast>
            <actor ref="P1"/>
            <actor ref="P2"/>
        </cast>
        <rate>
            <imdb>8.5</imdb>
            <rottentomatoes>8</rottentomatoes>
            <moviedb>7.4</moviedb>
            <average>7.97</average>
        </rate>
        <numOscars>2</numOscars>
    </movie>
</movies>

【问题讨论】:

    标签: sql xml database postgresql xmltype


    【解决方案1】:

    您可以将整个 XML 文档作为值存储在单个 xml 列中,也可以提取数据并将其存储在或多或少的 normalized form 中。

    哪个更好,取决于我们不知道的应用程序的所有细节。

    这是一个相关的答案,讨论了存储文档类型与数据库规范化的优缺点:

    【讨论】:

    • 我明白了。我添加了一个实例。可以看到,它或多或少是一个简单的结构,但是有些元素是被别人引用的,所以不知道能不能把它们分开……
    • @Fabio:没有明确的答案。您必须根据应用程序的完整情况来决定采用哪种方式。我添加了指向相关答案的链接。
    • 谢谢,我去看看
    【解决方案2】:

    将 XML 保存为 DB 的文本列,以便您也可以轻松应用相等运算符。您可能会在插入 " 或 ' 时发现一些错误,因此请尝试用 ~ 或 ` 等其他字符替换它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-05
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多