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

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.

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;
          } 

Why is there an additional br/ after the /p tag? If you need spacing, CSS is the way to go.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.