Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Ways to parse an XML using PL/SQL

user10873676Dec 19 2018 — edited Dec 19 2018

Hi All,

What are the ways to parse an xml using pl/sql.

Does Oracle provides some in built support for parsing XML, are there any libraries we can use for this purpose.

Thanks,

Ankit

Comments

BluShadow

Yes, Oracle does provide functionality for dealing with XML.

What did you find when you googled for it?  There's plenty of examples out there and plenty of information on this community too.

You could even check out the XMLDB space on this community: https://community.oracle.com/community/developer/english/oracle_database/xml_db   which has lots of information, questions and examples of dealing with XML.

People on the community are more than happy to help you deal with specific issues you may have, but it always helps if you show that you've made an effort in the first place, otherwise you'll just end up upsetting people.

Billy Verreynne

Parsing XML is as simple as instantiating a XMLTYPE object, by passing it the text data (varchar2 or clob) via its default constructor.

Basic example:

SQL> declare

  2          xml     XMLtype;

  3  begin

  4          -- valid xml

  5          xml := new XMLtype( '<xml><data>Hello world</data></xml>' );

  6  end;

  7  /

PL/SQL procedure successfully completed.

SQL>

SQL> declare

  2          xml     XMLtype;

  3  begin

  4          -- invalid xml

  5          xml := new XMLtype( '<xml><data>Hello world</xml>' );

  6  end;

  7  /

declare

*

ERROR at line 1:

ORA-31011: XML parsing failed

ORA-19202: Error occurred in XML processing

LPX-00225: end-element tag "xml" does not match start-element tag "data"

Error at line 1

ORA-06512: at "SYS.XMLTYPE", line 310

ORA-06512: at line 5

SQL>

Paulzip

user10873676 wrote:

Hi All,

What are the ways to parse an xml using pl/sql.

Does Oracle provides some in built support for parsing XML, are there any libraries we can use for this purpose.

Thanks,

Ankit

Yes, Oracle is feature rich in handling XML, parsing, constructing, querying, transforming, storing, indexing.  Much of which is part of the SQL engine.

Here's a simple example of extracting data from XML...

select a.doc_name, b.node_id, b.node_name

from  XMLTable('/doc'

                passing XmlType(

                '<doc name="test">

                  <node id="1">Node1</node>

                  <node id="2">Node2</node>

                </doc>')

      columns

        doc_name varchar2(40) path '@name',

        nodes    xmltype      path 'node'

      ) a,

      XMLTable('/node'

                passing a.nodes

      columns

        node_id   varchar2(4)  path '@id',

        node_name varchar2(10) path '.'

      ) b

                        

DOC_NAMENODE_IDNODE_NAME
test1Node1
test2Node2
unknown-7404
What are the ways to parse an xml using pl/sql.

Does Oracle provides some in built support for parsing XML, are there any libraries we can use for this purpose.

Yes - Oracle provides some in built support for parsing XML.

Yes - Oracle provides EXTENSIVE documentation that describes ALL of its functionality.

Yes - you can EASILY find that documentation by doing a SIMPLE web search using your EXACT words

'oracle 18c What are the ways to parse an xml using pl/sql'

jaramill

user10873676 wrote:

Hi All,

What are the ways to parse an xml using pl/sql.

Does Oracle provides some in built support for parsing XML, are there any libraries we can use for this purpose.

Thanks,

Ankit

READ the documentation by searching for it on the web:

Here's the XML Developer's Kit Programmer's Guide https://docs.oracle.com/database/121/ADXDK/toc.htm

Here's the Database PL/SQL Packages and Types Reference

DBMS_XML* packages located here --> https://docs.oracle.com/database/121/ARPLS/toc.htm

pastedImage_2.png

1 - 5

Post Details

Added on Dec 19 2018
5 comments
387 views