问题描述
将XML数据(从网页中获取)插入PostgreSQL数据库的最佳方法是什么?
我正在使用Java,需要一些帮助,以找到一种将这些数据读入数据库的好方法.
What would be the best way to insert XML data (that I get from a webpage) into PostgreSQL database?
I'm using Java and need a little help finding a good way to read this data into the database.
推荐答案
Postgres有(感谢Daniel Lyons指出),可用于存储表.但是,如果您想手动切碎XML数据,则有多种可能性可以在数据库中表示XML数据.第一个问题应该是,如果您想要一个非常通用的解决方案,那么它将能够存储任何XML文档或特定于您的域的XML文档(即,仅允许具有特定结构的XML文档).因此,您将获得一个非常灵活的通用表示形式,但是很难查询(所需的SQL将会非常复杂).如果您有更具体的方法,则查询会更简单,但是每次您要存储另一种类型的文档或向现有文档添加字段时,都需要创建新表或向现有目录添加新属性;因此更改架构将更加困难(这是XML的一大优势). 此演示文稿应该为您提供一些想法,不同的可能性是什么
Postgres has (thanks to Daniel Lyons for pointing it out) native XML support which you can use to store your table. If you however want to shred your XML data manually, there are different possibilities to represent XML data in a database. The first question should be, if you want a very generic solution, that will be able to store any XML document or one that is specific to your domain (i.e. only allows XML documents of a certain structure). Depending on that, you will have a very flexible, universal representation which is however harder to query (the SQL needed will be quite complicated). If you have a more specific approach, the queries will be simpler, but you will need to create new tables or add new attributes to existing talbes every time you want to store another type of document or add a field to an existing document; so changing the schema will be harder (which is one major advantage of XML). This presentation should give you some ideas what are the different possibilities.
此外,您可能会考虑切换到一些支持Xquery的数据库,例如 DB2 一个>.使用针对处理XML的语言XQuery进行本地查询的能力将大大简化工作.
Also, you might consider to switch to some DB that supports Xquery, like DB2. The ability to natively query using XQuery, a language targeted at processing XML, will simplify things a lot.
更新:给出您的评论,您的XML数据(即您链接到的 )是完全相关的.可以将1:1映射到下表:
UPDATE: Given your comment, your XML data (that you linked to) is perfectly relational. It can be mapped 1:1 to the following table:
CREATE TABLE mynt (
ID SERIAL ,
myntnafn CHAR(3) ,
myntheiti Varchar(255) ,
kaupgengi Decimal(15,2) ,
midgengi Decimal(15,2) ,
solugengi Decimal(15,2) ,
dagsetning TimeStamp
)
因此,任何mynt
标记都将是表中的记录,而相应的子标记则是属性.我从您的数据中收集的数据类型可能是错误的.主要问题是IMO,因为没有自然主键,所以我添加了一个自动生成的主键.
So any mynt
tag would be a record in the table and the corresponding sub-tags the attributes. The data types I gathered from your data, they might be wrong. The main problem is, IMO, that there is no natural primary key, so I added an autogenerated one.
这篇关于如何使用Java从PostgreSQL数据库中的现有数据库中创建新的XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!