2

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?

1
  • You can escape mark-up using the backticks character
    – Nic Gibson
    Mar 8, 2011 at 11:16

2 Answers 2

2

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;

1

This should work:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.