将 XML 存储到 Postgres

Storing XML into Postgres

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

我从来没有在数据库中使用过 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>

您可以将整个 XML 文档存储为单个 xml column or you can extract data and store it in a more or less normalized form 中的值。

哪个更好,取决于我们不知道的所有申请细节。

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

将XML另存为数据库的文本列,这样您也可以轻松应用相等运算符。您可能会在插入 " 或 ' 时发现一些错误,因此请尝试将它们替换为其他字符,例如 ~ 或 `,两者都是。