Hey everyone, I am having trouble with a simple task and its driving me nuts. I need to load the contents of an XML file into an array so I can loop through it and insert them into my database. I need to have the name attribute of <word>(ie: "work title") and the contents of <word> (ie: "this is the data") in separate variables/arrays. Can anybody help me with this? I cant seem to set up my script right to pull them into variables. I am starting over and thought I would come here first, before spending another day of scripting.

XML Example:
<glossary>
<word name="work title">
<![CDATA[This is the data.]]>
</word>
<word name="work title">
<![CDATA[This is the data.]]>
</word>
<word name="work title">
<![CDATA[This is the data.]]>
</word>
</glossary>

Thanks in advance.

    Have you looked at the [man]SimpleXML[/man] extension?

      Thanks for your reply Nogdog

      I have tried to use simpleXML but its not retrieving the CDATA. I can get the name attributes, but not the actual contents of <word>. Any idea on how to allow the simpleXML to read the contents located in the CDATA?

      Thanks again for your reply

        You could load the data with the LIMXML_NOCDATA parameter:

        <?php
        error_reporting(E_ALL);
        
        $text = <<<EOD
        <glossary>
        <word name="work title">
        <![CDATA[This is the data.]]>
        </word>
        <word name="work title">
        <![CDATA[This is the data.]]>
        </word>
        <word name="work title">
        <![CDATA[This is the data.]]>
        </word>
        </glossary>
        EOD;
        
        $xml = simplexml_load_string($text, 'SimpleXMLElement', LIBXML_NOCDATA);
        foreach($xml->word as $word)
        {
           echo "<pre>";
           print_r($word);
           echo "</pre>\n";
        }
        

          Here is a very simple code that can work for my application if I can get it to find the CDATA. Again, this is a very simple code and I know it is not compensating for the CDATA tags because it is not echoing it. I have never worked with XML before, so I may be way off-base here.
          Thank you for any replays.

          $xmlstr = <<<XML
          <?xml version='1.0' standalone='yes'?>
          <glossary>
          <word name="work title">
          <![CDATA[This is the data.]]>
          </word>
          <word name="work title">
          <![CDATA[This is the data.]]>
          </word>
          <word name="work title">
          <![CDATA[This is the data.]]>
          </word>
          </glossary>
          XML;

          $xml = new SimpleXMLElement($xmlstr);

          foreach ($xml->children() as $second_gen) {
          echo ' Word: ' . $second_gen['name']. '<br>';

          foreach ($second_gen->children() as $third_gen) {
              echo ' Definition: ' . $xml['word'] . ';'; // this is not locating the CDATA
          }

          }

            Nogdog,
            That is working great and is loading the data into usable variables - thank you. I do have one problem I could really use more help on with your code. By what name should I call the CDATA contents. Here is what I am using - I modified your code to work with my task

            <?
            error_reporting(E_ALL); 
            
            include 'glossary.php';
            
            $xml = new SimpleXMLElement($xmlstr);
            
            $text = $xmlstr;
            
            $xml = simplexml_load_string($text, 'SimpleXMLElement', LIBXML_NOCDATA); 
            foreach($xml->word as $word) 
            
            // { 
              // echo "<pre>"; 
              // print_r($word); 
              // echo "</pre>\n"; 
            // }
            
             echo "".$word['name']." - ".$word['0']."<br>";
            ?>
            

            When I fun your script it does read the CDATA and it assigns it to [0]. In my adaptation I am trying to call it by using $word['0'], but that is not working. How can I call the CDATA? Calling the $word['name'] is working great.

            Thanks again

              It's a bit non-intuitive, yet ultimately rather simple:

              foreach($xml->word as $word)
              {
                 echo "<p>".$word['name']." - ".$word."</p>\n";
              } 
              

                Thank you again for your time. That worked great!

                  An alternative is to use the standard [man]DOM[/man] interface.

                  $xml = new DOMDocument;
                  $xml->loadXML($text);
                  $word_data = array();
                  foreach($xml->getElementsByTagName('word') as $word)
                  {
                  	$word_data[] = $word->nodeValue;
                  }
                  print_r($word_data);
                  

                    Hello Everyone,
                    Nogdog the script worked great and that data has been taken care of. I do have one more file that I need to insert that is a little more complicated, but I think can use the same principle. I need to load the data into variables that I can work with. I have one area that is giving me a problem and I wanted to see if anybody can help. Here is my new xml data breakdown that I am working with.

                    <topics>
                    <item id="1" answer_id="B">
                    <question><![CDATA[This is where the Main data is located]]></question>
                    <replies>
                    <reply id="A"><![CDATA[Answer Option A]]> </reply>
                    <reply id="B"><![CDATA[Answer Option B]]>
                    <description><![CDATA[Explination of question]]></description>
                    </reply>
                    <reply id="C"><![CDATA[Answer Option C]]></reply>
                    <reply id="D"><![CDATA[Answer Option D]]></reply>
                    </replies>
                    </item>
                    </topics>

                    I can get all of the data loaded into variable except for all four of the <reply>'s. I can find the first one, but not the following three others. Can anybody let me know what I am doing wrong. How should I be calling the data for these <reply> items?

                    Here is the php code I am working with.

                    <?php
                    
                    error_reporting(E_ALL); 
                    
                    include 'xmlFile.php';
                    
                    $xml = new SimpleXMLElement($xmlstr);
                    $text = $xmlstr;
                    $xml = simplexml_load_string($text, 'SimpleXMLElement', LIBXML_NOCDATA); 
                    foreach($xml->item as $item)  {
                    	foreach ($item->question as $question) {
                    	foreach ($item->replies as $replyGroup) {
                    			foreach ($replyGroup->reply as $replyMain) { // This gives me the first reply but not the rest
                    				foreach ($replyMain->description as $desc) {
                    				}
                    			}
                     	}
                    	}
                    
                    echo "Situation : ".$question."<br>Correct Reponse: ".$replyMain['id']."<br>Why: ".$desc."<br>Reply One: ".$replyMain."<br>Reply Two: <br>Reply Three: <br>Reply Four: <br><br>";
                    
                    }
                    
                    
                    ?>

                    Thanks in advance

                      Write a Reply...