postgresqlHow do I parse XML data using PostgreSQL?
PostgreSQL can parse XML data using the xml
data type and several functions and operators that are available to work with it.
To parse XML data, you need to first convert it into the xml
data type. This can be done using the xmlparse
function. For example:
SELECT xmlparse(document '<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>');
The output of this code will be:
xmlparse
---------------
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
(1 row)
Once the XML data is in the xml
data type, you can use the xpath
function to extract values from the XML. For example:
SELECT xpath('/note/to/text()', xmlparse(document '<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>'));
The output of this code will be:
xpath
-------
Tove
(1 row)
You can also use the xmlexists
function to check if a certain node exists in the XML. For example:
SELECT xmlexists('/note/from[text() = "Jani"]'
PASSING BY VALUE xmlparse(document '<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>'));
The output of this code will be:
xmlexists
-----------
t
(1 row)
Helpful links
More of Postgresql
- How can I extract the year from a PostgreSQL timestamp?
- How can I use PostgreSQL with YAML?
- How do I use PostgreSQL's XMIN and XMAX features?
- How can I use PostgreSQL and ZFS snapshots together?
- How do I use a PostgreSQL XML parser in an example?
- How can I use PostgreSQL's UPSERT feature?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How can I use PostgreSQL for my project?
- How do I decide whether to use PostgreSQL VARCHAR or TEXT data types?
- How do I use the WITH statement in PostgreSQL?
See more codes...