Oracle xmltable解析返回LPX-00209

标签 oracle xpath oracle11g xml-parsing xmltable

我是 Oracle 的新手。现在我正在尝试解析 XML 并将其放入 VK_ACCOUNTS 表中。 我的 xml 示例在这里:

<?xml version="1.0" encoding="utf-8"?>
<response list="true">
 <account>
  <account_id>1656672360</account_id>
  <account_type>general</account_type>
  <account_status>1</account_status>
  <access_role>reports</access_role>
 </account>
</response>

我使用下面的函数请求 xml,据我所知它正确返回 xml:

create or replace function GET_CLOBFROMURL(
  p_url     varchar2,
  p_charset varchar2 default 'UTF8'
) return clob
is
  req   utl_http.req; 
  resp  utl_http.resp;
  val   varchar2(32547);
  a     clob;
BEGIN
  a:='';
  dbms_lob.createtemporary(a,true);
  dbms_lob.open(a,dbms_lob.lob_readwrite);
  req := utl_http.begin_request(p_url);
  utl_http.set_body_charset(req, p_charset);
  resp := utl_http.get_response(req);
  LOOP
    a := a||val;
    utl_http.read_text(resp, val, 5000);
  END LOOP;
  utl_http.end_response(resp);
  return a;
EXCEPTION
  WHEN utl_http.end_of_body THEN
    utl_http.end_response(resp);
    return a;
  WHEN others then
    utl_http.end_response(resp);
    raise;
END;

并将 xml 获取到 xml_clob

xml_clob := tableau.get_clobFromUrl(REQUEST);
    IF xml_clob != EMPTY_CLOB() THEN
    insert into tableau.VK_ACCOUNTS(account_id, account_type, account_status, access_role)
        SELECT
        proc.account_id,
        proc.account_type,
        proc.account_status,
        proc.access_role   
        FROM XMLTABLE('response/account' passing (select xmltype(xml_clob)  resp FROM dual)
            columns account_id number             path '/account/account_id',
                    account_type varchar2(20)     path '/account/account_type',
                    account_status number         path '/account/account_status',
                    access_role varchar2(20)      path '/account/access_role'
                ) proc;
      COMMIT;     
    END IF;
END;

最后我得到一个错误:

ORA-31011: XML parsing failed
ORA-19202: Error occurred in XML
LPX-00209: PI names starting with XML are reserved
Error at line 3
ORA-06512: на  "SYS.XMLTYPE", line 272
ORA-06512: на  line 1
ORA-06512: на  "TABLEAU.VK_LOADDATA", line 11
ORA-06512: на  line 2

我已经尝试过 XML 的 TRIM 函数,但它对我没有帮助。

有什么想法吗?

最佳答案

LPX-00209: PI names starting with XML are reserved

当 XML 字符串中存在前导空白字符时,就会出现这种情况。 See http://oraclequirks.blogspot.co.uk/2013/03/lpx-00209-pi-names-starting-with-xml.html .

您应该能够通过使用以下方法解决它:

XMLType( TRIM( xml_clob ) )

稍微简化你的功能:

create or replace function GET_CLOBFROMURL(
  p_url     varchar2,
  p_charset varchar2 default 'UTF8'
) return clob
is
  req   utl_http.req; 
  resp  utl_http.resp;
  val   varchar2(32547);
  a     clob    := EMPTY_CLOB();
BEGIN
  req := utl_http.begin_request(p_url);
  utl_http.set_body_charset(req, p_charset);
  resp := utl_http.get_response(req);
  LOOP
    BEGIN
      utl_http.read_text(resp, val, 5000);
      a := a||val;
    EXCEPTION
      WHEN utl_http.end_of_body THEN
        utl_http.end_response(resp);
        EXIT;
    END;
  END LOOP;
  return a;
EXCEPTION
  WHEN others THEN
    utl_http.end_response(resp);
    RAISE;
END;

然后插入可以完全用 SQL 完成:

insert into tableau.VK_ACCOUNTS (
  account_id,
  account_type,
  account_status,
  access_role
)
SELECT proc.account_id,
       proc.account_type,
       proc.account_status,
       proc.access_role   
FROM   ( SELECT tableau.get_clobFromUrl(REQUEST) AS xml FROM DUAL ) c,
       XMLTABLE(
         '/response/account'
          passing XMLType( TRIM( c.xml ) )
          columns account_id     number       path './account_id',
                  account_type   varchar2(20) path './account_type',
                  account_status number       path './account_status',
                  access_role    varchar2(20) path './access_role'
            ) proc
WHERE  c.xml IS NOT NULL
AND    DBMS_LOB.GETLENGTH( c.xml ) > 0;

关于Oracle xmltable解析返回LPX-00209,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41693602/

相关文章:

database - Oracle 触发器无效

sql - Oracle SQL 查询中的方括号是什么意思?

java - SQL异常 : Protocol Violation in oracle

javascript - 无效状态错误: DOM Exception 11 on XPath results

xml - 使用 XPath 在格式不佳的 XML 中查找 XML 同级

mysql - 如何处理where子句中的非ascii字符

java - 基于现有值在主键列中插入新值,保持唯一性约束和最大长度约束

oracle - 根据登录分配 Oracle APEX 5 中的第一页

c++ - ODBC SQLDriverConnect 未连接

xml - 带条件的 xpath 计数