解析XML使用PL / SQL输出html中特定标记的内容

时间:2013-02-08 13:29:13

标签: xml plsql xml-parsing

我目前在使用PL / SQL解析某些XML时遇到问题。基本上我有一些XML存储在现有函数提供的clob变量中。我创建了一个xmltype的新变量,用于将clob转换为xmltype。然后我遍历这个新的xmltype变量的内容,试图拉出每个标签的内容并将其输出到它自己的div中。但是我的代码目前只是将所有标签的内容输出到一个div中,这给人的印象是它根本没有循环。

XML结构(显然有更多嵌套的内部标签):

 <?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>

我可以在这里找到我用来循环的PLSQL:

    -- 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,'&lt;','<');
        l_html_build := replace(l_html_build,'&gt;','>');
        l_html_build := replace(l_html_build,'&amp;lt;','');
        l_html_build := replace(l_html_build,'&amp;gt;','');
        l_html_build := replace(l_html_build,'&amp;nbsp;','&nbsp;');
        l_html_build := l_html_build ||'</body></html>';
        l_parse_xml_index := l_parse_xml_index + 1; 
     end loop;  

可以在下面找到一些参数和变量的解释:

    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

输出如下:

 <div class = "description">
  Description 1 here Description 2 here
 </div> 

应该是:

 <div class = "description">
  Description 1 here
 </div> 
 <div class = "description">
  Description 2 here
 </div> 

任何帮助都会非常感激,因为我是一名html / css / php / javascript程序员,我之前并没有真正完成PL / SQL(我的老板还没有真正提供任何真正的培训帮助)。

提前致谢!

2 个答案:

答案 0 :(得分:1)

这似乎比jme1988的解决方案更有效率 - 至少在玩具示例上有~1000 return个记录。显然我不能说这种方法将如何扩展到您正在使用的数据量(但是,它应该可以很好地扩展)。

DECLARE
   l_c CLOB;
BEGIN
   l_html_build   := '<!DOCTYPE html><html><head></head><body>';
   l_parse_xml    := xmltype(l_xml_response);
   --
   BEGIN
      WITH xdata AS
         (
            SELECT xmltype ( l_xml_response )   xml
              from dual
         )
    SELECT XMLSERIALIZE ( CONTENT extract(xml, '//description') )  x
      INTO l_c
      FROM xdata
         ;
   END;
   l_html_build := l_html_build || l_c;
   --
   l_html_build := replace(l_html_build,'<description', '<div class="description"');
   l_html_build := replace(l_html_build,'</description>', '</div>');
   l_html_build := replace(l_html_build,'&amp;lt;','&lt;');
   l_html_build := replace(l_html_build,'&amp;gt;','&gt;');
   l_html_build := replace(l_html_build,'&amp;nbsp;','&nbsp;');
   l_html_build := l_html_build ||'</body></html>';
END;

希望这些东西满足您的需求。问候,卡斯滕

答案 1 :(得分:0)

A_horse_with_no_name的建议是正确的,但更有效的方法是:

     begin
          for value_set_rec in 
            (select extract(value(x),'/description/text()').getclobval() l_description
                 from table(xmlsequence(xmltype(l_xml_response).extract('//description')))x) 
            loop
              l_html_build := l_html_build || '<div class="description">';
              l_html_build := l_html_build || value_set_rec.l_description;
              l_html_build := l_html_build || '</div>';     
            end loop;

            exception
                when others then
                     --Error handling here
      end;    

        l_html_build := replace(l_html_build,'&lt;','<');
        l_html_build := replace(l_html_build,'&gt;','>');
        l_html_build := replace(l_html_build,'&amp;lt;','');
        l_html_build := replace(l_html_build,'&amp;gt;','');
        l_html_build := replace(l_html_build,'&amp;nbsp;','&nbsp;');
        l_html_build := l_html_build ||'</body></html>';