0

Oracle version 11g. I have a working SQL query in a PL/SQL procedure. What is the easiest way to take this query and output XML, either specifying the element names, or taking them from the query columns? I'm confused about whether to use DBMS_XMLGEN, XMLQuery or SQLX. What is the canonical way of doing this in Oracle?

Thanks

2

1 Answer 1

4

Check the SQL Functions generating XML http://docs.oracle.com/cd/B19306_01/appdev.102/b14259/xdb13gen.htm#i1029583. For example:

SELECT
   XMLRoot(
      XMLElement(
        "employees", 
        XMLAgg(
           XMLElement("employee",
              XMLConcat(
                 XMLElement(
                    "name",
                    e.name
                 ),
                 XMLElement(
                    "surname",
                    e.surname
                 )
              )
           )
        )
     ), VERSION '1.0', STANDALONE YES
  ).getClobVal() INTO retval -- useful if you want to export it outside oracle
  FROM employees e

should return for example:

<?xml version="1.0" standalone="yes"?>
<employees>
    <employee>
        <name>Jan</name>
        <surname>Kowalski</surname>
    </employee>
    <employee>
        <name>Tomasz</name>
        <surname>Nowak</surname>
    </employee>
</employees>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Better late than never.
useful answer, but kindly can I set alias for the result column instead of that strange long name ??? because when i set an alias for it I got result in (HUGECLOB) but I need string result to be shown

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.