将DOMElement转换为字符串,然后删除不需要的标记

时间:2010-11-14 15:03:47

标签: php xml string dom

现在,感谢社区,修复了一个问题(Delete all elements of a certain type from an XML doc using PHP)。现在我幸运地击中了其他人:)

所以,我有XML文件:

<piletilve_info>
   <shows>
      <show>
        <description>
          <lat>
        <![CDATA[ 
LA CENERENTOLA  <BR><BR>

("Cinderella")<BR><BR>

Opera buffo by Gioachino Rossini<BR><BR>


Music Director and Conductor: Arvo Volmer<BR>
Stage Director and Set Designer: Michiel Dijkema (Amsterdam)<BR>
Costumes: Claudia Damm (Berlin)<BR><BR>



Premiere on November 10, 2006<BR>
Approx running time: 3 h 20 min<BR>
Sung in Italian with subtitles in Estonian and English<BR><BR>

Who hasn’t heard the fairy-tale of Cinderella who crouched in the ashes, danced to the tune of her step-sisters but finally found the man of her dreams and a happy life. Why should this fairy-tale be told today?<BR>
An exciting and unexpected solution of the story of Cinderella with striking scenery and fantastic-modern costumes is presented by the Dutch stage director Michiel Dijkema and the German designer Claudia Damm.<BR>
In this story, an unhappy soul dreams of a prince on a white horse and of a better life, and women following the last trends chase the prince, and a man who considers himself very wise, is only interested in himself.<BR>
"Cinderella" is not just a fairy-tale, it includes reality as well as dreams, a lot of fantasies, comedy and grotesque. And all of this is expressed by the brilliant and virtuous music of Rossini!<BR>

  ]]> 
          </lat>
        </description>
      </show>
   </shows>
   <other node>
      ...
   </other node>
</piletilve_info>

我使用以下代码导航到我需要的节点:

$books = $xpath->query('shows/show/description');
        foreach($books as $description)
        {
            $description->removeChild($eng_lang);
            $lat_lang = $description->getElementsByTagName('lat')->item(0);
            }

我想使用此混合函数输出已修改的$lat_lang元素,即<[[CDATA ]]> parent,content:

$lat_lang->nodeValue = mb_convert_encoding(print_r(strip_tags_only($lat_lang->item(0)->nodeValue, TRUE),'<html><head><body>'), 'UTF-8' ,$encoding);

,但是我失败了,当我在apache服务器上执行脚本时,我收到了这个错误:

  

无法显示XML页面   无法使用XSL样式查看XML输入   片。请更正错误   然后单击“刷新”按钮,或者尝试   稍后再试。

     
     

只允许使用一个顶级元素   在XML文档中。处理错误   资源   “http://www.bilesuserviss.lv/lv_filtrs_ ...

     

可捕获的致命错误:对象   类DOMElement不能   在/ var / www /...

中转换为字符串

你能帮我找到解决方案吗?

也许我在delphi上开发的这个事实让我对整个PHP&lt; - &gt; XML编码更加困难,对于普通人来说...... :(

1 个答案:

答案 0 :(得分:2)

这是拼写问题。 函数名称是strip_only_tags(),但我用strip_tags_only()调用它;

我整天都在研究delphi和php&lt;&gt; xml项目..有点感觉已经累了:( 对不起。

也向戈登致敬。有点叫醒我:D