如何将xml数据添加到postgresql数据库

时间:2011-10-08 07:14:59

标签: asp.net-mvc-2 postgresql npgsql

下面的肥皂响应包含产品表,大约5000行。 产品表(如下)与xml数据几乎相似。

产品表需要每小时从xml数据更新。

如何将此xml数据添加到产品表中?

我应该使用PostgreSql xpath()函数还是其他任何ides? 在ASP .NET / Mono中使用npgsql和C#。

CREATE TABLE products (
SupplierCode char(20) primary key,
SegmentId char(8),
GroupId char(8),
ClassId char(8),
SeriesId char(8),
VendorId char(2),
PartNumbrt char(27),
Name Text,
Warranty Numeric(6,2),
Price Numeric(10,4),
Quantity Numeric(8,2)
)
添加所需的肥皂响应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xmlns:xsd="http://www.w3.org/2001/XMLSchema"; xmlns:soap12="http://www.w3.org/2003/05/soapenvelope";>
<soap12:Body>
<GetProductListResponse xmlns="http://xxx.yy.zz/";>
<GetProductListResult>
<ProductList>
<Product>
<SupplierCode>001982</SupplierCode>
<SegmentId>65000000</SegmentId>
<GroupId>65010000</GroupId>
<ClassId>65010200</ClassId>
<SeriesId>10001125</SeriesId>
<VendorId>AM</VendorId>
<PartNumber>ADA3000BIBOX</PartNumber>
<Name>AMD Athlon64 3000+ (1800MHz/L2 Cache 512KB) Socket 939, BOX</Name>
<Warranty>36</Warranty>
<Price>196.00000</Price>
<Quantity>0</Quantity>
<DateExpected>1999-01-01T00:00:00</DateExpected>
<IsNewProduct>true</IsNewProduct>
</Product>
<Product>
<SupplierCode>001512</SupplierCode>
<SegmentId>65000000</SegmentId>
<GroupId>65010000</GroupId>
<ClassId>65010200</ClassId>
<SeriesId>10001125</SeriesId>
<VendorId>AM</VendorId>
Acme API Specification v 1.0
13
<PartNumber>ADA3000AXBOX</PartNumber>
<Name>AMD Athlon64 3000+ (2000MHz/1600MHz/L2 Cache 512KB) Socket 754, BOX</Name>
<Warranty>36</Warranty>
<Price>296.00000</Price>
<Quantity>0</Quantity>
<GrossWeight>3.6000</GrossWeight>
<DateExpected>1999-01-01T00:00:00</DateExpected>
<IsNewProduct>false</IsNewProduct>
</Product>
</ProductList>
</GetProductListResult>
</GetProductListResponse>
</soap12:Body>
</soap12:Envelope>

1 个答案:

答案 0 :(得分:1)

为了简化操作,我首先将XML导入临时表:

CREATE TABLE xml_import
(
   xml_data  xml
)

然后,一旦填充了临时表,就可以使用SQL语句从中检索数据,将XML转换为关系表示:

with product_list as (
  select unnest(xpath('/soap12:Envelope/soap12:Body/pl:GetProductListResponse/pl:GetProductListResult/pl:ProductList/pl:Product', xml_data, 
          ARRAY[ array['xsd', 'http://www.w3.org/2001/XMLSchema-instance'], 
                 array['soap12', 'http://www.w3.org/2003/05/soapenvelope'], 
                 array['pl', 'http://xxx.yy.zz/']])) as product
  from xml_import
)
select (xpath('/Product/SupplierCode/text()', product)::varchar[])[1] as suppliercode, 
       (xpath('/Product/SegmentId/text()', product)::varchar[])[1] as segmentid,
       (xpath('/Product/PartNumber/text()', product)::varchar[])[1] as partnumber,
       to_number((xpath('/Product/Price/text()', product)::varchar[])[1], '99999.99999') as price,
       to_number((xpath('/Product/GrossWeight/text()', product)::varchar[])[1], '9999.9999') as weight
from product_list

我没有费心包括所有列,但我想你得到的照片。

我会将上述语句放入视图中,然后您只需使用该视图填充您的真实产品表即可。

如果您不想创建该临时表,您可以将所有内容放在一个语句中:

with xml_import (xml_data) as ( 
   select '.... xml goes here '::xml
), 
product_list as (
  ... same as above ...
)
select ... same as above ...
from product_list