0

Hi all,

I need to parse an XML file, which includes CDATA and HTML <br> tag I would like to include for my output.

I would like to replace <br> with <br /><br /> - Two spaces, but it doesnt seem to happen.

This is an example on the XML file, and how I am parsing it. Is there another way so that I can simple output the data, and not replace anything. (But I would like to replace <br> with two <br /><br /> still..)

XML file:

    <body>
    <![CDATA[
    TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT.
    ]]>
    <br/>
    <![CDATA[
    TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT.
    ]]>
    <br/>
    <![CDATA[
    TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT.
    ]]>
    <br/>
    <![CDATA[
    TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT.
    ]]>
    <br/>
    <![CDATA[
    TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT.
    ]]>
    <br/>
    <![CDATA[
    TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT.
    ]]>
    <br/>
    <![CDATA[
    TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT. TEXT TEXT.
    ]]>
    <br/>
    </body>

I am getting the data like this, works fine - But I am not sure if I am doing it the smartest way - And as mentioned, I would like to replace br with two line breaks form the xml file.

    if($XML_file = @simplexml_load_file($XML_url))
    {
        $data = str_replace(array('<![CDATA[', ']]>'), array('',''), $XML_file->asXML());
    }
    else $data = '';
    return $data;

That returns the data, with line breaks <br> - But I cant seem to replace <br> with to <br /> when I use the returned data from another variable outside the method.

Any comments on another approach to parse XML file with simplexml, that includes CDATA?

Regards, Klemme

Edited by klemme

2
Contributors
3
Replies
11
Views
3 Years
Discussion Span
Last Post by pritaeas
0

Am wondering, why you are using SimpleXML if you do nothing with it? You can achieve the above with a file_get_contents. If you use xpath to get the value of body you shouldn't have to replace all the cdata tags.

0

Hmm right, I managed to do it this way - Any comments on whether my approach is "optimal", is appreciated - as I havent worked a lot woth XML data yet.

$data = array();
          // Hent Marketing description fra XML fil hos CNET
          if( !empty($XML_url) )
          {
              $XML = new DOMDocument();
              $XML->load($XML_url);
              $tags = $XML->getElementsByTagName('body');
              foreach ($tags as $tag) 
              {
                  foreach($tag->childNodes as $child) 
                  {
                      if ($child->nodeType == XML_CDATA_SECTION_NODE) 
                      {
                          $data[] .= '<p>' . $child->textContent . '</p>
                          <br />';
                      }
                  }
              }
              return $data;
          } 

Edited by klemme

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.