简体   繁体   中英

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". "data" contains large XML-data.

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". So it'll look like <fw:MyID>ID57841</fw:MyID> .

Also, I would like to output certain XML-elements from that column, say <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.

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

Btw: to inlcude < and > in your post, you can use the HTML entities &lt; and &gt;

This should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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