在postgresql中导入xml数据

时间:2016-12-28 11:44:38

标签: xml postgresql postgresql-9.1

我尝试从XML插入到postgresql表数据 这是示例XML:

<?xml version="1.0" encoding="UTF-8"?>
<ActualStatuses>
   <ActualStatus ACTSTATID="0" NAME="Not actual" />
   <ActualStatus ACTSTATID="1" NAME="Актуальный" />
</ActualStatuses>

要加载XMl,我使用此功能:

CREATE OR REPLACE FUNCTION bytea_import(IN p_path text, OUT p_result bytea)
  RETURNS bytea AS
$BODY$
declare
  l_oid oid;
  r record;
begin
  p_result := '';
  select lo_import(p_path) into l_oid;
  for r in ( select data 
             from pg_largeobject 
             where loid = l_oid 
             order by pageno ) loop
    p_result = p_result || r.data;
  end loop;
  perform lo_unlink(l_oid);
end;$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION bytea_import(text)
  OWNER TO postgres;

对于从XML到postgresql表的插入值,我使用精简查询:

INSERT INTO actualstatuses(
    SELECT
        (xpath('//ActualStatus/@ACTSTATID', myTempTable))[1]::text::bigint AS ACTSTATID,
        (xpath('//ActualStatus/@NAME', myTempTable))[1]::text AS NAME
    FROM
        unnest(xpath('//ActualStatus', convert_from(public.bytea_import('C:/fias/update/AS_ACTSTAT.XML'), 'utf8')::xml)) AS myTempTable);

并且有解析器错误:

invalid XML content
SQL-status: 2200N
Entity: line 1: parser error : XML declaration allowed only at the start of the document
<?xml version="1.0" encoding="utf-8"?><AddressObjectTypes><AddressObjectType 

但如果我删除XML中的<?xml version="1.0" encoding="utf-8"?>,这项工作就很棒了。我有大约20个XML文件,其中一些非常大。如何摆脱这个错误?

1 个答案:

答案 0 :(得分:0)

   INSERT INTO actualstatuses(
    SELECT
        (xpath('//ActualStatus/@ACTSTATID', myTempTable))[1]::text::bigint AS ACTSTATID,
        (xpath('//ActualStatus/@NAME', myTempTable))[1]::text AS NAME
    FROM
        unnest(xpath('//ActualStatus', replace(convert_from(bytea_import('C:/fias/update/AS_ACTSTAT.XML'), 'utf8'),'<?xml version="1.0" encoding="utf-8"?>','')::xml)) AS myTempTable);