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 开发者_Go百科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 < and >
This should work:
SELECT xpath('//fw:MyPassword/text()', CAST(convert_from(bytea_column, 'UTF-8') AS XML))
FROM your_table;
精彩评论