简体   繁体   English

如何从 Oracle SQL Developer 中提取 XML 数据

[英]How to extract XML data from Oracle SQL Developer

I'm trying to extract XML data from feedback table consist of message column;我正在尝试从包含消息列的反馈表中提取 XML 数据; below is the XML code:下面是 XML 代码:

 <?xml version="1.0" encoding="UTF-8"?> <message>Hi Hello</message> <channel>XYZ</channel> <sentiment-score>5.0</sentiment-score> <structured-fields> <structured-filed><name>NA_score</name><value>5</value></structured-filed> <structured-filed><name>NPS_score</name><value>10</value></structured-filed> <structured-fields>

I need to extract the above XML code from message column.我需要从消息列中提取上述 XML 代码。

Your XML document needs to have a single XML element as the document root :您的 XML 文档需要有一个XML 元素作为文档根

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <message>Hi Hello</message>
  <channel>XYZ</channel>
  <sentiment-score>5.0</sentiment-score>
  <structured-fields>
    <structured-field><name>NA_score</name><value>5</value></structured-field>
    <structured-field><name>NPS_score</name><value>10</value></structured-field>
  </structured-fields>
</root>

Once that is fixed then just use an XMLTable and XPath:一旦修复,只需使用XMLTable和 XPath:

SELECT *
FROM   XMLTABLE(
         '/root'
         PASSING XMLType( '<?xml version="1.0" encoding="UTF-8"?><root><message>Hi Hello</message><channel>XYZ</channel><sentiment-score>5.0</sentiment-score><structured-fields><structured-field><name>NA_score</name><value>5</value></structured-field><structured-field><name>NPS_score</name><value>10</value></structured-field></structured-fields></root>' )
         COLUMNS
         MESSAGE         VARCHAR2(200) PATH '//message',
         CHANNEL         VARCHAR2(200) PATH '//channel',
         SENTIMENT_SCORE NUMBER(3,1)   PATH '//sentiment-score',
         NA_SCORE        NUMBER(3,0)   PATH '//structured-fields/structured-field[name/text()="NA_score"]/value',
         NPS_SCORE       NUMBER(3,0)   PATH '//structured-fields/structured-field[name/text()="NPS_score"]/value'
       );

Output :输出

MESSAGE  | CHANNEL | SENTIMENT_SCORE | NA_SCORE | NPS_SCORE
:------- | :------ | --------------: | -------: | --------:
Hi Hello | XYZ     |               5 |        5 |        10

db<>fiddle here db<> 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM