将XML存储到Postgres [英] Storing XML into Postgres

查看:56
本文介绍了将XML存储到Postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文档,需要将其存储在SQL db(Postgres)中.我已经看到了如何完成此操作,但是我有一个问题:我是否只创建一个带有xml字段的表并将整个文档放置在那里?这是有关电影等(电影,演员...)的文档,其中的信息可供以后检索.

I have an XML document that needs to get stored in an SQL db (Postgres). I've already seen how that's done, but I have a question: do I just create a single table with a xml field and place the whole document there? This is a document about movies and so (movies, actors...) that has information to be later retrieved.

我从未在数据库中使用过XML,所以有点困惑.

I've never worked with XML in databases, so I'm a little confused.

这是我的XML的示例:

Here's an example of my 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文档作为值存储在单个

You can store a whole XML document as value in a single xml column or you can extract data and store it in a more or less normalized form.

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

Which is better, depends on all the details of your application that are unknown to us.

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

Here is a related answer discussing pros and cons of storing document types vs. db normalization:

这篇关于将XML存储到Postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆