Oracle 中的复杂 XML 处理

发布于 2024-10-15 16:28:05 字数 1232 浏览 2 评论 0原文

谁能告诉我如何在 oracle 中处理复杂的 XML?例如,我有下面的 XML:

<?xml version="1.0" encoding="utf-8" ?> 
<UserEmailPreferences>
 <EmailOffering>
  <EmailGroupID>1</EmailGroupID> 
  <EmailOfferingID>0</EmailOfferingID> 
  <Service>
    <val>service-1</val>
    <val>service-2</val>
  </Service> 
  <Title>Clinical Focus</Title> 
  <SubscriptionOption>D</SubscriptionOption> 
  </EmailOffering>
</UserEmailPreferences>
<UserEmailPreferences>
 <EmailOffering>
  <EmailGroupID>1</EmailGroupID> 
  <EmailOfferingID>0</EmailOfferingID> 
  <Service>
    <val>service-1</val>
  </Service> 
  <Title>XYZ</Title> 
  <SubscriptionOption>D</SubscriptionOption> 
  </EmailOffering>
</UserEmailPreferences>

在上面的 XML 中,我有两个 节点。它可以是两个、三个或任何其他......这是动态的。

节点中,我有 块,它对于相应的 节点又是动态的。

谁能帮助我如何在 Oracle 中处理此类 XML?

Can anyone please tell how I can process complex XML in oracle? For e.g I have the XML below:

<?xml version="1.0" encoding="utf-8" ?> 
<UserEmailPreferences>
 <EmailOffering>
  <EmailGroupID>1</EmailGroupID> 
  <EmailOfferingID>0</EmailOfferingID> 
  <Service>
    <val>service-1</val>
    <val>service-2</val>
  </Service> 
  <Title>Clinical Focus</Title> 
  <SubscriptionOption>D</SubscriptionOption> 
  </EmailOffering>
</UserEmailPreferences>
<UserEmailPreferences>
 <EmailOffering>
  <EmailGroupID>1</EmailGroupID> 
  <EmailOfferingID>0</EmailOfferingID> 
  <Service>
    <val>service-1</val>
  </Service> 
  <Title>XYZ</Title> 
  <SubscriptionOption>D</SubscriptionOption> 
  </EmailOffering>
</UserEmailPreferences>

In the above XML, I have two <UserEmailPreferences> nodes. It can be two, three or whatever...That is dynamic.

In <UserEmailPreferences> node, I have <Service> block which is again dynamic for corresponding <EmailOffering> node.

Can anyone please help me how I can process such XML in Oracle?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街道布景 2024-10-22 16:28:05

您可以使用SO问题 "Oracle Pl/SQL: Loop through 中描述的方法XMLTYPE 节点”,例如:

SQL> SELECT extractvalue(column_value,'/EmailOffering/EmailGroupID') "GrpID",
  2         extractvalue(column_value,'/EmailOffering/EmailOfferingID')"OfrID",
  3         extractvalue(column_value,'/EmailOffering/Title') "Title",
  4         extractvalue(column_value,'/EmailOffering/SubscriptionOption')"Op",
  5         extract(COLUMN_VALUE, '/EmailOffering/Service') service
  6    FROM TABLE(XMLSequence(XMLTYPE('<?xml version="1.0" encoding="utf-8"?>
  7  <document>
  8     <UserEmailPreferences>
  9      <EmailOffering>
 10       <EmailGroupID>1</EmailGroupID>
 11       <EmailOfferingID>0</EmailOfferingID>
 12       <Service>
 13         <val>service-1</val>
 14         <val>service-2</val>
 15       </Service>
 16       <Title>Clinical Focus</Title>
 17       <SubscriptionOption>D</SubscriptionOption>
 18       </EmailOffering>
 19     </UserEmailPreferences>
 20     <UserEmailPreferences>
 21      <EmailOffering>
 22       <EmailGroupID>1</EmailGroupID>
 23       <EmailOfferingID>0</EmailOfferingID>
 24       <Service>
 25         <val>service-1</val>
 26       </Service>
 27       <Title>XYZ</Title>
 28       <SubscriptionOption>D</SubscriptionOption>
 29       </EmailOffering>
 30     </UserEmailPreferences>
 31  </document>').extract('/document/UserEmailPreferences/EmailOffering'))) t;

GrpID  OfrID  Title                Op     SERVICE
------ ------ -------------------- ------ -------------------------------------
1      0      Clinical Focus       D      <Service>
                                            <val>service-1</val>
                                            <val>service-2</val>
                                          </Service>

1      0      XYZ                  D      <Service>
                                            <val>service-1</val>
                                          </Service>

请注意,您的 XML 不是有效文档,因为它缺少封闭标签(我添加了标签 /document

you can use the method described in the SO question "Oracle Pl/SQL: Loop through XMLTYPE nodes", for example:

SQL> SELECT extractvalue(column_value,'/EmailOffering/EmailGroupID') "GrpID",
  2         extractvalue(column_value,'/EmailOffering/EmailOfferingID')"OfrID",
  3         extractvalue(column_value,'/EmailOffering/Title') "Title",
  4         extractvalue(column_value,'/EmailOffering/SubscriptionOption')"Op",
  5         extract(COLUMN_VALUE, '/EmailOffering/Service') service
  6    FROM TABLE(XMLSequence(XMLTYPE('<?xml version="1.0" encoding="utf-8"?>
  7  <document>
  8     <UserEmailPreferences>
  9      <EmailOffering>
 10       <EmailGroupID>1</EmailGroupID>
 11       <EmailOfferingID>0</EmailOfferingID>
 12       <Service>
 13         <val>service-1</val>
 14         <val>service-2</val>
 15       </Service>
 16       <Title>Clinical Focus</Title>
 17       <SubscriptionOption>D</SubscriptionOption>
 18       </EmailOffering>
 19     </UserEmailPreferences>
 20     <UserEmailPreferences>
 21      <EmailOffering>
 22       <EmailGroupID>1</EmailGroupID>
 23       <EmailOfferingID>0</EmailOfferingID>
 24       <Service>
 25         <val>service-1</val>
 26       </Service>
 27       <Title>XYZ</Title>
 28       <SubscriptionOption>D</SubscriptionOption>
 29       </EmailOffering>
 30     </UserEmailPreferences>
 31  </document>').extract('/document/UserEmailPreferences/EmailOffering'))) t;

GrpID  OfrID  Title                Op     SERVICE
------ ------ -------------------- ------ -------------------------------------
1      0      Clinical Focus       D      <Service>
                                            <val>service-1</val>
                                            <val>service-2</val>
                                          </Service>

1      0      XYZ                  D      <Service>
                                            <val>service-1</val>
                                          </Service>

Note that your XML wasn't a valid document since it was missing an enclosing tag (I added the tag /document)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文