Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1382
  • Last Modified:

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

0
ciphersol
Asked:
ciphersol
1 Solution
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now