如何使用java从PostgreSQL数据库中的现有数据库创建新的XML文件 [英] how can I create new XML file from existing database in PostgreSQL database using java

查看:30
本文介绍了如何使用java从PostgreSQL数据库中的现有数据库创建新的XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将 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.使用 XQuery(一种旨在处理 XML 的语言)进行本地查询的能力将大大简化事情.

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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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