0

I am having an issue parsing an XML file. I have other PHP scripts that do this but for some reason this one just won't work.

XML Example:
<Data>
<Email>
      <Subject>Email</Subject>
      <Sender>Sender</Sender>
      <Recipients>'recipient1', recipient2</Recipients>
      <Date>2014-02-24T22:37:16Z</Date>
      <Size>16 KB</Size>
      <Url>URL</Url>
   </Email>
</Data>

and

PHP Example:
<?php
$file="data.xml";
$xml = simplexml_load_file($file) or die ('Cant Load XML');
echo "
<table>
<tr>
<th>Subject</th>
<th>Sender</th>
<th>Recipients</th>
<th>Date/Time</th>
<th>Size</th>
<th>Link</th>
</tr>
";
foreach($xml->Data as $data){
foreach($data->children() as $he => $email){
echo "<tr>";
echo "<td>{$email->Subject}</td>";
echo "<td>{$email->Sender}</td>";
echo "<td>{$email->Recipients}</td>";
echo "<td>{$email->Date}</td>";
echo "<td>{$email->Size}</td>";
echo "<td><a href={$email->URL}>Link</a></td>";
echo "<br />";
echo "</tr>";
}}
echo '</table>';

?>

I get the header rows but after that it seems like nothing processes. I do not get an error so it seems to be loading the XML. Not sure why this one is not working but similar scripts I have do. Maybe a fresh pair of eyes would help?

NDunn
  • 1
  • 1
  • Did you try adding `` at the top? – Scott Arciszewski Feb 28 '14 at 15:54
  • "This one wont work" isn't helpful. What exactly isn't working? Are you getting any error messages? What do you expect to happen and what is happening? We can't help you unless you tell us what is wrong. – Styphon Feb 28 '14 at 15:54
  • Does `var_dump($data->Email);` return anything? – MueR Feb 28 '14 at 15:55
  • Try example 1 in this link: http://us1.php.net/simplexml_load_file and see what happens – karthikr Feb 28 '14 at 15:55
  • `var_dump($xml)` would should you what you actually got. – Marc B Feb 28 '14 at 15:56
  • Is `Data` the root node? If so, you need to remove the inner loop and just do `foreach($xml->Email as $email)` – Michael Berkowski Feb 28 '14 at 15:58
  • I tried var_dump($xml) and got back what I expected. – NDunn Feb 28 '14 at 16:02
  • ... which is? a node containing ` ... ` ? because if that is the case and Email is a direct child of Data, you can just call `$data->Email` – MueR Feb 28 '14 at 16:03
  • foreach($xml->Email as $email) seems to have got it. I had thought I tried that -_- Thank you so much for your help everyone! – NDunn Feb 28 '14 at 16:04
  • @NDunn: Common beginner mistake, the XML root element (here: ``) is already represented by `$xml`. See as well http://php.net/simplexml.examples-basic – hakre Mar 03 '14 at 17:44
  • possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – Dejan Marjanović Mar 03 '14 at 17:45

0 Answers0