www.webdeveloper.com
Results 1 to 4 of 4

Thread: Can PHP Read "CData" Info?

  1. #1
    Join Date
    Sep 2008
    Posts
    260

    Can PHP Read "CData" Info?

    Hello again.

    Lets say that was reading an XML file and I wanted to pull and save to a variable the text only info in between these tags:

    Code:
    <description><![CDATA[ some text, some text, and some text [...]]]></description>
    How would I go about doing that with PHP? Is it even possible?

  2. #2
    Join Date
    Aug 2006
    Posts
    1,897
    Sure, I'd use SimpleXML:

    Code:
    $x = simplexml_load_file ("content.xml");
    $desc = $x->description;
    The CDATA begin/end stuff isn't going to end up in $desc.

    Dave

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,154
    You should be able to access it via the DOM classes, e.g.:
    PHP Code:
    <?php
    $xml 
    = <<<EOD
    <?xml version='1.0' ?>
    <document>
     <thing>
      <description><![CDATA[The first thing]]></description>
     </thing>
     <thing>
      <description><![CDATA[The second thing]]></description>
     </thing>
    </document>
    EOD;

    $dom = new DOMDocument();
    $dom->loadXML($xml);
    $descriptions $dom->getElementsByTagName('description');
    foreach(
    $descriptions as $desc)
    {
       echo 
    "<p>".$desc->textContent."</p>\n";
    }
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Sep 2008
    Posts
    260
    ...alright..appreciate it...I just thought the "CDATA" stuff would get in the way...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles