2

I Have xml :

<?xml version="1.0" encoding="UTF-8"?>
<attachments>
  <entry file="cewe_gw.jpg" name="cewe_gw.jpg"/>
  <entry file="wp1827515.png" name="wp1827515.png"/>
</attachments>

I want to get list file? ex:

cewe_gw.jpg
wp1827515.png
1
  • 3
    Why the Java tag? Are you looking for a SQL solution or Java solution? Commented Dec 1, 2020 at 7:01

2 Answers 2

1

If you are looking for a Postgres solution, then you can use xmltable for this:

select x.*
from the_table t
  cross join xmltable('/attachments/entry'
                      passing t.the_xml_column
                      columns file text path '@name') as x

This returns the value of the attribute name, if you want the file attribute, you need to change @name to @file

Online example

Sign up to request clarification or add additional context in comments.

3 Comments

an ERROR: syntax error at or near "passing"
Wow, never knew about xmltable. Thanks for showing.
@sahroni then you are using an outdated Postgres version
0

demo:db<>fiddle

To select any element or attribute, you can use XPath:

SELECT 
    xpath('//entry/@file',xml)
FROM mydata

This returns an array of file attributes. You can extract them using unnest()

SELECT 
    unnest(xpath('//entry/@file',xml))
FROM mydata

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.