简体   繁体   English

从包含在 bytea 列中的 XML 数据的表中选择

[英]SELECT from table containing XML-data in bytea-column

I have a PostgreSQL database containing a table with a column of type bytea named "data".我有一个 PostgreSQL 数据库,其中包含一个名为“data”的 bytea 类型列的表。 "data" contains large XML-data. “数据”包含大型 XML 数据。

I would like to be able to search for specific rows, where there is a XML-element in "data" called <fw:MyID></fw:MyID> that contains "ID57841".我希望能够搜索特定行,其中“数据”中有一个名为<fw:MyID></fw:MyID>的 XML 元素,其中包含“ID57841”。 So it'll look like <fw:MyID>ID57841</fw:MyID> .所以它看起来像<fw:MyID>ID57841</fw:MyID>

Also, I would like to output certain XML-elements from that column, say <fw:MyPassword></fw:MyPassword> .另外,我想从该列输出某些 XML 元素,例如<fw:MyPassword></fw:MyPassword>

I cannot write in the database, only read.我无法写入数据库,只能读取。 I've Googled after answers a lot, but cannot fint anything that helps me.我在谷歌上搜索了很多答案,但找不到任何对我有帮助的东西。 Can someone please help me?有人可以帮帮我吗?

You should be able to convert the bytea column to a text column "on-the-fly" using convert_from() and then apply an xpath() function on the result.您应该能够使用convert_from()将 bytea 列“即时”转换为文本列,然后对结果应用 xpath() 函数。

Something like:就像是:

SELECT xpath('/fw:MyPassword/text()', convert_from(bytea_column, 'UTF-8'))
FROM your_table;

You will most likely need to supply the namespace as a third parameter.您很可能需要提供命名空间作为第三个参数。
Check out the manual for details regarding this:查看手册以了解有关此内容的详细信息:
http://www.postgresql.org/docs/current/static/functions-xml.html#FUNCTIONS-XML-PROCESSING http://www.postgresql.org/docs/current/static/functions-xml.html#FUNCTIONS-XML-PROCESSING

Btw: to inlcude < and > in your post, you can use the HTML entities &lt;顺便说一句:要在您的帖子中包含 < 和 >,您可以使用 HTML 实体 &lt; and &gt;和 &gt;

This should work:这应该有效:

SELECT xpath('//fw:MyPassword/text()', CAST(convert_from(bytea_column, 'UTF-8') AS XML))
FROM your_table;

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

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