2

I have php code connecting to an API:

require(dirname(__FILE__) . '/MadMimi.class.php');
$mailer = new MadMimi('XXX.org', '000');
$lists = $mailer->Lists();
var_dump($lists);
foreach ($lists as $list) {
echo $list['name']; }

var_dump shows a XML doc in the form:

`string '<?xml version="1.0" encoding="UTF-8"?>
<lists>
  <list id="8" name="#1" subscriber_count="210" display_name="Display name"/>
  <list id="9" name="#2" subscriber_count="2242" display_name="Display name"/>`

...(length=2726)

but it only shows the first few lines. How do I make it show all the values?

Also I'm trying to print specific values using the foreach loop but it isn't working and I'm getting the error: Invalid argument supplied for foreach()

If foreach doesn't work how else can I obtain values from the XML doc?

If I do

$xml=simplexml_load_string($lists) or die("Error: Cannot create object"); 
print_r($xml);

I get:

SimpleXMLElement Object ( [list] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 8 [name] => #1 [subscriber_count] => 210 [display_name] => Display name ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 9 [name] => #2 [subscriber_count] => 2242 [display_name] => Display name ) )

how do I write a foreach loop for this?

Alex
  • 16,739
  • 1
  • 28
  • 51
LTech
  • 1,677
  • 5
  • 29
  • 48

2 Answers2

1

I'm not sure how your XML looks like but you can read XML in php:

$file = "path to XML";

$xml=simplexml_load_file($file) or die("Error: Cannot create object");
print_r($xml);

This will return a simple XML object.

If your XML contains sub-children then you can use a foreach loop to traverse it.

Max
  • 1,810
  • 3
  • 26
  • 37
rajatsaurastri
  • 653
  • 3
  • 21
  • I've added my print_r($xml) to the original question. I can't figure out the foreach loop. If you could help me that would be appreciated. – LTech Apr 30 '15 at 10:07
  • your get an array object try to beautify your array and you will get your array like this SimpleXMLElement Object ( [list] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 8 [name] => #1 [subscriber_count] => 210 [display_name] => Display name ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 9 [name] => #2 [subscriber_count] => 2242 [display_name] => Display name ) ) Now go through children and traverse it. If you are not sure with traversing then I recommend to read about array traversing – rajatsaurastri Apr 30 '15 at 10:17
  • you can try echo '
    '; print_r($xml); echo '
    '; so that it would be much readable for you and would be easy to understand and traverse
    – rajatsaurastri Apr 30 '15 at 10:19
1

It will help you.

$lists = '<?xml version="1.0" encoding="UTF-8"?>
<lists>
  <list id="8" name="#1" subscriber_count="210" display_name="Display name"/>
  <list id="9" name="#2" subscriber_count="2242" display_name="Display name"/>
</lists>';


$xml = simplexml_load_string($lists) or die("Error: Cannot create object");

foreach ($xml->children() as $child) {
     // List all the tags
    echo '<pre>';print_r($child);echo '</pre>';

    foreach($child->attributes() as $a => $b) {
    // list all the attributes
        echo $a,'="',$b,"\"\n";
    }
}
Noman
  • 4,088
  • 1
  • 21
  • 36
  • I pasted your code from try { $listing... but I got an error: PHP Parse error: syntax error, unexpected 'catch' (T_CATCH) – LTech Apr 30 '15 at 10:06
  • I have tested, and it working fine for me. make sure you have copy correctly. – Noman Apr 30 '15 at 10:08
  • I don't have a file source = test.xml, it is on the Madmimi Api. Is this why it isn't working? – LTech Apr 30 '15 at 10:11