Ed Heal
Ed Heal

Reputation: 59997

dbms_xmldom - How to get the <?xml tag

I am trying to get the line <?xml ....?> at the start of the XML document using the PL/SQL Package dbms_xmldom. Here is the code so far

declare
  l_dom   dbms_xmldom.DOMDocument;
  l_clob  clob;
  l_node  dbms_xmldom.DOMNode;

begin
  l_dom := dbms_xmldom.newDomDocument;
  l_node := dbms_xmldom.makeNode(l_dom);

  l_node := dbms_xmldom.appendChild(l_node, 
              dbms_xmldom.makeNode(
                dbms_xmldom.createElement(l_dom, 'root')
              )
            );

  dbms_lob.createtemporary(l_clob, true);

  dbms_xmldom.writeToClob(l_dom, l_clob);
  dbms_output.put_line(l_clob);
end;

The output is:

<root/>

Expect:

<?xml version="1.0" encoding="UTF-8"?>
<root/>

Any pointers on how to do this would get great.

Upvotes: 1

Views: 1035

Answers (2)

paulzip
paulzip

Reputation: 86

The prolog is usually added automatically by XML serialization, so you shouldn't need to add it yourself, but if you want to you can with XMLRoot.

Your method for generating XML is quite inefficient. You should be looking at XMLElement, XMLForest, XMLAgg etc.

Here's a simple root and child example, with prolog in one line of code.

select XMLRoot(XMLElement("root", XMLElement("child", 12)), version '1.0') from dual

<?xml version="1.0"?>
<root>
  <child>12</child>
</root>

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 59997

Just for the record - here is what you need to add

dbms_xmldom.setVersion(l_dom, '1.0" encoding="UTF-8');

after creating the document

Upvotes: 2

Related Questions