Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get XML element value in Oracle DBMS_XMLDOM package

I have XML file that's stored in the clob column of the table in DB.

<?xml version="1.0" encoding="UTF-8"?>
<document>
  <row>
    <organization>asdklfjas;kldfj;LASKJFAS</organization>
    <phones>sjhdfhjaghjskfg</phones>
    <persons>hkjg</persons>
  </row>                                                  
</document>

I'm using DBMS_XMLDOM package for parse it.

declare
  v_clob clob;
  v_doc dbms_xmldom.domdocument;
begin
  ...
  v_doc := dbms_xmldom.newdomdocument(v_clob);
  v_domelement := dbms_xmldom.getdocumentelement(v_doc);
  ...
end;

I need simply to get value from some element, for example <persons>. How can I do it?

like image 623
sergdenisov Avatar asked Jul 30 '13 14:07

sergdenisov


2 Answers

There is also the option to use XslProcessor functions together with XPath:

DECLARE
  v_Clob CLOB;
  v_Doc XmlDom.DomDocument;
  v_RootNode XmlDom.DomNode;
  v_Value VARCHAR2(128);
BEGIN
  ...

  v_Doc := XmlDom.NewDomDocument(v_Clob);
  v_RootNode := XmlDom.MakeNode(XmlDom.GetDocumentElement(v_Doc));
  v_Value := XmlDom.GetNodeValue(
    XslProcessor.SelectSingleNode(v_RootNode, '/document/row[1]/persons/text()'));

  ...
END;
like image 64
blerontin Avatar answered Sep 19 '22 06:09

blerontin


I found what I was searching:

...
v_doc := dbms_xmldom.newdomdocument(v_clob);
v_nodelist := dbms_xmldom.getelementsbytagname(v_doc, 'persons');
v_node := dbms_xmldom.getfirstchild(dbms_xmldom.item(v_nodelist, 0));
v_person := dbms_xmldom.getnodevalue(v_node);
...
like image 28
sergdenisov Avatar answered Sep 19 '22 06:09

sergdenisov