7

How do you work with the oracle XML data type?

This question is intended to be asked and answered by me, just to share the information with others

6
  • 1
    BAD JOB. Either get a blog, or make this community wiki!!! Commented Oct 20, 2010 at 5:36
  • 3
    How do you make it a "community wiki"? I was under the impression that doing this sort of thing was encouraged, as per the stackoverflow faq Commented Oct 20, 2010 at 5:39
  • It should still be a question, though. "How do I work with" is a bit broad. +1 anyway. Commented Oct 20, 2010 at 6:08
  • You probably would not have gotten slammed for it without the statement of intention. Commented Oct 20, 2010 at 6:10
  • @astandar please read: meta.stackexchange.com/questions/392/… Commented Oct 21, 2010 at 5:03

1 Answer 1

12

The following sample SQL demonstrates how to insert, query and update database fields that contain the XMLTYPE data type:

-- Create a table that can store XML
create table sample_xml (id number, xml xmltype);

-- Insert some XML into the table
insert into sample_xml values (1, xmltype.createxml('<subject><name>test</name><list><li>a</li><li>b</li></list></subject>'));
insert into sample_xml values (2, xmltype.createxml('<subject><name>test</name><list><li>a</li></list></subject>'));

-- When doing a select, refer to table using the alias or getClobVal() will not work
select t.id, t.xml.getClobVal() from sample_xml t;

-- Update text of a single xml element
UPDATE sample_xml SET xml = UPDATEXML(xml, '/subject/name/text()','happy') WHERE id = 2;

-- Select out just the name for each row in the table using xpath
select id, extractvalue(xml, '/subject/name/text()') from sample_xml;

-- Doing an sample_xml, where the xpath string matches two elements, the text of both elements will be updated
UPDATE sample_xml SET xml = UPDATEXML(xml, '/subject/list/li/text()','one');
Sign up to request clarification or add additional context in comments.

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.