Storing XML into Postgres

2019-09-02 01:55发布

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.

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

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>

1条回答
贪生不怕死
2楼-- · 2019-09-02 02:42

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.

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

查看更多
登录 后发表回答