Hello Experts,
When a node text value is null, how do I update a node text value using dbms_xmldom.setNodeValue() method? The code below runs in 10g. The <NAME> node updates. But, the <NAME2> node does not. Do i need to add a text node to <NAME2> first? How? Is there another way?
declare
var XMLType;
buf clob;
l_xmldoc dbms_xmldom.DOMDocument;
l_root dbms_xmldom.DOMNode;
l_node dbms_xmldom.DOMNode;
begin
var := xmltype('<PERSON> <NAME>ChangeMe</NAME> <NAME2></NAME2> </PERSON>');
-- Create DOMDocument handle:
l_xmldoc := dbms_xmldom.newDOMDocument(var);
l_root := dbms_xmldom.makeNode(l_xmldoc);
-- before
dbms_xmldom.writetobuffer(l_root, buf);
dbms_output.put_line('Before:');
dbms_output.put_line(buf);
-- replace name.text
l_node := dbms_xslprocessor.selectSingleNode(l_root,'//NAME/text()');
if dbms_xmldom.isnull(l_node) then
dbms_output.put_line('IsNull:');
dbms_xmldom.setNodeValue(l_node, 'node is null');
else
-- Manipulate:
dbms_xmldom.setNodeValue(l_node, 'raj');
end if;
-- replace name2.text... how is this done when the text node is null?
l_node := dbms_xslprocessor.selectSingleNode(l_root,'//NAME2/text()');
if dbms_xmldom.isnull(l_node) then
dbms_output.put_line('IsNull: And, setNodeValue() does nothing to <NAME2>');
dbms_xmldom.setNodeValue(l_node, 'node is null');
else
-- Manipulate:
dbms_xmldom.setNodeValue(l_node, 'node is not null');
end if;
-- after
dbms_xmldom.writetobuffer(l_root, buf);
dbms_output.put_line('After:');
dbms_output.put_line(buf);
end;
the code above yields the following output:
Before:
<PERSON>
<NAME>ChangeMe</NAME>
<NAME2/>
</PERSON>
IsNull: And, setNodeValue() does nothing to <NAME2>
After:
<PERSON>
<NAME>raj</NAME>
<NAME2/>
</PERSON>
DECLARE
var XMLType;
doc dbms_xmldom.DOMDocument;
docelem dbms_xmldom.DOMElement;
PROCEDURE create_or_replace_text_nod
nodelist dbms_xmldom.DOMNodelist;
node dbms_xmldom.DOMNode;
childnode dbms_xmldom.DOMNode;
BEGIN
nodelist := dbms_xmldom.getElementsByT
node := dbms_xmldom.item(nodelist,
IF (dbms_xmldom.hasChildNodes
childnode := dbms_xmldom.getFirstChild(
dbms_xmldom.setNodeValue(c
ELSE
childnode := dbms_xmldom.makeNode(dbms_
childnode := dbms_xmldom.insertBefore(n
END IF;
END create_or_replace_text_nod
BEGIN
var := xmltype('<PERSON><NAME>Ori
doc := dbms_xmldom.newDOMDocument
docelem := dbms_xmldom.getDocumentEle
-- Extract from the XMLType the before values...
dbms_output.put_line('Old values...');
dbms_output.put_line(var.e
dbms_output.put_line(var.e
create_or_replace_text_nod
create_or_replace_text_nod
-- Extract from the XMLType the after values...
dbms_output.put_line('NEW VALUES...');
dbms_output.put_line(var.e
dbms_output.put_line(var.e
END;