Oracle PL/SQL display values from XML Text

I want to display specific data values from XML text. I'm not getting an error but the results are not displaying.  In the sample text below, I want to loop through the different "Modifier" tags and display the "Code" value.

Results:
l_result: 3
l_result:
l_result:
l_result:

Code:
set serveroutput on;
declare 
   l_parser    dbms_xmlparser.Parser;
   l_doc       dbms_xmldom.DOMDocument;
   l_n         dbms_xmldom.DOMNode;

   l_nlexpa    dbms_xmldom.DOMNodeList;
   l_nlexpa2    dbms_xmldom.DOMNodeList;
   l_result    VARCHAR2(50);
   xml varchar2(4000) := '<Modifiers><Modifier Code="1" Description="One"/><Modifier Code="2" Description="Two"/><Modifier Code="3" Description="Four"/></Modifiers>';

begin
   l_parser := dbms_xmlparser.newParser;
   dbms_xmlparser.parseBuffer(l_parser, xml);
   l_doc := dbms_xmlparser.getDocument(l_parser);
   
   l_nlexpa  := dbms_xslprocessor.selectNodes(dbms_xmldom.makeNode(l_doc),'/Modifiers/Modifier');
   dbms_output.put_line('l_result: '||dbms_xmldom.getLength(l_nlexpa));
   
   for cur_tab in 0 .. dbms_xmldom.getlength(l_nlexpa) - 1 loop
      l_n := dbms_xmldom.item(l_nlexpa, cur_tab);

      dbms_xslprocessor.valueof(l_n,'Code/text()',l_result);
      dbms_output.put_line('l_result: '||l_result);
   end loop;
   
   dbms_xmldom.freeDocument(l_doc);
   dbms_xmlparser.freeParser(l_parser);
   
end;
/

Open in new window

ciphersolAsked:
Who is Participating?
 
ciphersolAuthor Commented:
I figured it out.

Solution:
set serveroutput on;
declare 
   l_parser    dbms_xmlparser.Parser;
   l_doc       dbms_xmldom.DOMDocument;
   l_n         dbms_xmldom.DOMNode;
   l_map       dbms_xmldom.DOMNamedNodeMap;
   attr        dbms_xmldom.DOMAttr;
   node        dbms_xmldom.DOMNode;
   v_text      varchar2(1000);
               
   --declare for capturing concatable nodes
   l_nlexpa    dbms_xmldom.DOMNodeList;
   l_nlexpa2    dbms_xmldom.DOMNodeList;
   l_result    VARCHAR2(50);
   xml varchar2(4000) := '<Modifiers><Modifier Code="1" Description="One"/><Modifier Code="2" Description="Two"/><Modifier Code="3" Description="Three"/></Modifiers>';

begin
   l_parser := dbms_xmlparser.newParser;
   dbms_xmlparser.parseBuffer(l_parser, xml);
   l_doc := dbms_xmlparser.getDocument(l_parser);

   l_nlexpa  := dbms_xslprocessor.selectNodes(dbms_xmldom.makeNode(l_doc),'/Modifiers/Modifier');
   dbms_output.put_line('l_result: '||dbms_xmldom.getLength(l_nlexpa));

   for cur_tab in 0 .. dbms_xmldom.getlength(l_nlexpa) - 1 loop
      l_n := dbms_xmldom.item(l_nlexpa, cur_tab);
      l_map :=  dbms_xmldom.getattributes(l_n);

      node := dbms_xmldom.getnameditem(l_map,'Code');
      v_text := dbms_xmldom.getnodevalue(node);      
      dbms_output.put_line(v_text);
      node := dbms_xmldom.getnameditem(l_map,'Description');
      v_text := dbms_xmldom.getnodevalue(node);
      dbms_output.put_line(v_text);
   end loop;

   dbms_xmldom.freeDocument(l_doc);
   dbms_xmlparser.freeParser(l_parser);

end;
/

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.