Question : How to parse XML string in Oracle procedure ?

How to parse the incoming XML string in Oracle stored procedure and how to Loop through the XML?

Answer : How to parse XML string in Oracle procedure ?

Ok How about this?
We will insert the xml into a table and retrieve that

Below Sample will extract the "privilege ID"

CREATE TABLE xml_test
(
   id          NUMBER DEFAULT 1 ,
   xml_text    VARCHAR2 (4000 BYTE),
   ts_update   DATE DEFAULT SYSDATE
)

SET DEFINE OFF;
Insert into XML_TEST
   (ID, XML_TEXT, TS_UPDATE)
 Values
   (2, '<page>
  <id>72054439</id>
  <redactions>
    <redaction>
      <id>0</id>
      <privileges>
        <privilege>
              <id>164</id>
              <Code>ACP</Code>
         </privilege>
               <privilege>      
                  <id>165</id>
                  <Code>AWPD</Code>
               </privilege>
      </privileges>
     </redaction>
     <redaction>
       <id>0</id>
        <privileges>
          <privilege>
                   <id>164</id>
                  <Code>ACP</Code>
            </privilege>
            <privilege>
                  <id>165</id>
                  <Code>AWPD</Code>
            </privilege>
      </privileges>
    </redaction>
  </redactions>
</page>', SYSDATE);
COMMIT;

SELECT x2.id
FROM xml_test t,
     XMLTable(
      '/page/redactions/redaction/privileges/privilege/id'
      passing XMLTYPE(t.xml_text)
      columns
        id number path '.'
     ) x2
WHERE t.id = 2  

Would give you the below output


PRIVILEGE_ID

164
165
164
165


You can use the similar approach to get the other tags also

Let me know if you have any questions
Random Solutions  
 
programming4us programming4us