簡體   English   中英

如何從 Oracle SQL Developer 中提取 XML 數據

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

我正在嘗試從包含消息列的反饋表中提取 XML 數據; 下面是 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>

我需要從消息列中提取上述 XML 代碼。

您的 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>

一旦修復,只需使用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'
       );

輸出

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

db<> 在這里擺弄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM