views:

889

answers:

2
  1. What is an XMLTABLE.

  2. Do let me know the syntax of creating an XMLTABLE

  3. Sample query to fetch records from XMLTABLE.

  4. Are there any database level pre-requisites required before creating an XMLTABLE.

A: 

XMLTABLE Might be what you'Re looking for.

Mac
A: 

The function XMLTABLE is used to translate an xml object into separated fields. But you probably want to construct a table with xml content, which is different.

You can create a table with an extra column that contains xml content

CREATE TABLE mytable (my_id NUMBER PRIMARY KEY, my_xml XMLType);

Then you use the xml content inside your queries.

INSERT INTO mytable VALUES (1,xmltype('<myxml id="D45"/>'));

SELECT my_id
      ,my_xml.extract('/myxml@id').getstringval()
from mytable

Finished.

OK responding to the second comment:

So you actually do want to use the XMLTABLE function, Your error indicates that you are not getting the file at all. So you need to craft your url to load it correctly. A test case I constructed with embedded xml is:

 1  SELECT seq
 2        , id
 3        , content
 4  FROM XMLTABLE('/xml/myrec'
 5        PASSING XMLType('<xml>'
 6                      ||'<myrec id="D12"><content>hello1</content></myrec>'
 7                      ||'<myrec id="D13"><content>hello2</content></myrec>
 8                      ||</xml>')
 9        COLUMNS   seq FOR ORDINALITY
10                , id VARCHAR2(100) PATH '@id'
11                , content VARCHAR2(100) PATH 'content'
12*      ) AS my_table

Output is:

 SEQ ID    CONTENT
---- ----- --------------------
   1 D12   hello1
   2 D13   hello2
Andrew Russell
Hi,xquery 1 does not work and give me the below error:SQL> xquery 1SP2-0042: unknown command "xquery 1" - rest of line ignored.Can you let me know the reason?
Sanjay Thakur
select TITLE, SOURCE, to_number(PRICE) as PRICE from XMLTABLE('/prices/book' passing HTTPURITYPE 'file:/D:/Sanjay_Thakur/book.xml').getXML() columns TITLE varchar2(100) path '/book/title',SOURCE varchar2(100) path '/book/source',PRICE varchar2(40) path '/book/price' )This query give me below error:[1]: (Error): ORA-29273: HTTP request failed ORA-06512: at "SYS.UTL_HTTP", line 1674 ORA-29261: bad argument ORA-06512: at "SYS.HTTPURITYPE", line 34 ORA-06512: at "SYS.HTTPURITYPE", line 97Can you let me know the reason?
Sanjay Thakur
Hi Andrew Russell,Thanks for your reply. The test case written by you above works fine at my end. Also, as per the example given by me (as mentioned in my comment earlier), the file does opens up if I give the path directly in the browser (i.e. 'file:/D:/Sanjay_Thakur/book.xml) as the file is present in the said path. It also opens up on double-click on the file. Just wondering why the example given by me above does not work. It gives me error "bad argument" pointing to the fuction 'getXML()'.
Sanjay Thakur
Using the path directly in a browser is not the same as the oracle database being able to see it.The url is a local url "file:", and the utl_http package and HTTPURITYPE is only for the http: protocol.You need to find another way to get the file into the database, read up on loading xml files into the database (http://stackoverflow.com/questions/998055/oracle-loading-a-large-xml-file).
Andrew Russell