2017-08-13 24 views
0

我有从Oracle原子提要返回的xml,我暂时将其存储在名为'ATOM'的apex_collection中。使用Oracle SQL解析XML内部的JSON

的XML看起来是这样的:

<?xml version="1.0" encoding="UTF-8"?> 
<feed xmlns="http://www.w3.org/2005/Atom"> 
<id>atomserver:newhire:feed</id> 
<title type="text">New Hire</title> 
<link href="https://ucf1-fap1273-hcm.oracledemos.com/hcmCoreApi/atomservlet/employee/newhire" rel="self" /> 
<updated>2013-12-16T18:17:56.108Z</updated> 
<entry> 
<link href="https://ucf1-fap1273-hcm.oracledemos.com/hcmCoreApi/atomservlet/employee/newhire/EMP300000133185858" rel="edit" /> 
    <id>atomservlet:newhire:EMP300000133185858</id> 
    <title type="text">Ford, Laura Hired</title> 
    <updated>2016-10-20T12:45:03.000Z</updated> 
    <author> 
    <name>BETTY.ANDERSON</name> 
    </author> 
    <summary type="text">Employee Period of Service Created</summary> 
    <published>2016-10-20T12:45:03.000Z</published> 
    <link href="https://ucf5-fap0357-hcm.oracledemos.com:443/hcmCoreApi/resources/latest/emps?q=PersonId=300000133184715&amp;effectiveDate=2015-01-01" rel="related" /> 
    <content type="text">{ "Context" : [ { "PrimaryPhoneNumber" : "44 1 781895862", "PersonId" : "300000133184715", "PersonName" : "Ford, Laura", "EffectiveStartDate" : "2015-01-01", "EffectiveDate" : "2015-01-01", "WorkerType" : "EMP", "PeriodType" : "E", "PersonNumber" : "3686", "WorkEmail" : "[email protected]" } ] }</content> 
</entry> 
</feed> 

到目前为止,我的SQL查询是这样的,它工作正常:

with t as (select clob001 from apex_collections where collection_name = 'ATOM') 
SELECT title, summary, content FROM t, 
    XMLTable(XMLNamespaces( 
      'http://www.w3.org/2005/Atom' AS "ns0" 

     ), 'ns0:feed/ns0:entry'    
      PASSING XMLTYPE.createXML(t.CLOB001) 
      COLUMNS title VARCHAR2(4000) path 'ns0:title' , 
        summary VARCHAR2(240) path 'ns0:summary', 
        content VARCHAR2(4000) path 'ns0:content'); 

image of result

,但我需要的信息即在内容标签内显示,就好像它的JSON一样。我知道如何用SQL解析JSON,但我不知道如何解析JSON,如果它在XML文档中。

+1

我不理解。此时'content'已经从XML中提取为关系数据。那么,为什么你不能用你已有的方法来解析? Whact在你尝试时发生? –

+0

内容标签的内容是JSON。所以如果我想标题,摘要和WorkEmail我想要做这样的事情:用t as(从apex_collections其中collection_name ='ATOM'选择clob001)选择标题,摘要,content.Context [0] .WorkEmail FROM ... .etc等,但是抛出我ORA-00923:FROM关键字找不到预期的地方。 –

回答

1

Oracle的JSON点符号(例如content.Context[0].WorkEmailis pretty strict。这不是在这里工作,因为(a)你没有在开始时放置表别名(你的XMLTable甚至没有表别名),但(b)更重要的是,你的content列的类型是VARCHAR2(4000)而不是JSON,它没有IS_JSON检查约束。我不认为你甚至可以添加一个约束到XMLTable。所有这些东西都需要使用点符号。

根据您的评论,更简单的选择是使用JSON函数,例如,

with t as (select clob001 from apex_collections where collection_name = 'ATOM') 
SELECT title, summary, 
     json_value(content, '$.Context[0].WorkEmail') as work_email 
    FROM t, 
    XMLTable(XMLNamespaces( 
      'http://www.w3.org/2005/Atom' AS "ns0" 
     ), 'ns0:feed/ns0:entry'    
      PASSING XMLTYPE.createXML(t.CLOB001) 
      COLUMNS title VARCHAR2(4000) path 'ns0:title' , 
        summary VARCHAR2(240) path 'ns0:summary', 
        content VARCHAR2(4000) path 'ns0:content'); 
+0

非常感谢。我认为这是正确的。我运行的其中一个数据库仍然在11.2,所以我无法访问json_value,但是我忽略了在我的帖子中提到这一点。但是这对于我们最终升级的时候非常有用。 –