I'm currently having issues parsing some XML using PL/SQL. Essentially I have some XML which is stored in a clob variable supplied by an existing function. I create a new variable of xmltype which is used to convert the clob to an xmltype. I then loop through the contents of this new xmltype variable in an attempt to pull out the content of each of the tags and output it in its own div. However my code currently just outputs the contents of all of tags into just one div which gives the impression that it is not looping through at all.
The XML structure is (obviously with more internal tags nested in return):
<?xml version="1.0" encoding="UTF-8"?>
<results>
<return>
<createDate> Date 1 Here </createDate>
<description> Description 1 here </description>
</return>
<return>
<createDate> Date 2 Here </createDate>
<description> Description 2 here </description>
</return>
</results>
And the PLSQL I am using to loop through can be found here:
-- parsing : Parse the xml and wrap description content in a html div
l_html_build := '<!DOCTYPE html><html><head></head><body>';
l_parse_xml := (xmltype(l_xml_response));
l_parse_xml_index := 1;
while l_parse_xml.existsNode('//description[' || To_Char(l_parse_xml_index) || ']') > 0
loop
l_html_build := l_html_build || '<div class="description">';
l_html_build := l_html_build || (xmltype(l_xml_response).extract('//description[' || To_Char(l_parse_xml_index)
|| ']/text()').getclobval());
l_html_build := l_html_build || '</div>';
l_html_build := replace(l_html_build,'<','<');
l_html_build := replace(l_html_build,'>','>');
l_html_build := replace(l_html_build,'&lt;','');
l_html_build := replace(l_html_build,'&gt;','');
l_html_build := replace(l_html_build,'&nbsp;',' ');
l_html_build := l_html_build ||'</body></html>';
l_parse_xml_index := l_parse_xml_index + 1;
end loop;
An explanation of some of the parameters and variables can be found below:
l_xml_response clob; -- XML from another function is stored here
l_html_build clob; -- Used as a variable to store the output to be sent to the page
l_parse_xml xmltype; -- l_xml_response content is passed into this xmltype variable which is used for parsing
l_parse_xml_index number; -- Used as an index to assist with looping through the tags
The output is as follows:
<div class = "description">
Description 1 here Description 2 here
</div>
When it should be:
<div class = "description">
Description 1 here
</div>
<div class = "description">
Description 2 here
</div>
Any help would be greatly appreciated as I'm a html/css/php/javascript programmer by trade and I've not really done PL/SQL before (And my boss hasn't really provided any real training which doesn't help).
Thanks in advance!
dbms_outputbut you can run that locally)