jonathanbyers
asked on
Parsing Nested XML With PHP
Our monitoring service has an API that I am trying to parse the XML from. I checked out magicparser.com which seems to be able to output it correctly, but can't for the life of me figure out how to output what I need.
I'm fairly new to XML with PHP, but have been using PHP for over a decade.
Ant point in the right direction would be appreciated.
Attached is a snippet of the API output.
alerts.xml
I'm fairly new to XML with PHP, but have been using PHP for over a decade.
Ant point in the right direction would be appreciated.
Attached is a snippet of the API output.
alerts.xml
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@Roads_Roads
This is what I have so far, which is perfect, but I need to show all of the information, not just for client[0].
Ray, can you expand on this with foreach()?
This is what I have so far, which is perfect, but I need to show all of the information, not just for client[0].
Ray, can you expand on this with foreach()?
<?php
error_reporting(E_ALL);
$xml = simplexml_load_file("alerts.xml");
echo '<pre>';
//print_r($xml);
echo $xml->items->client[0]->clientid . " | ";
echo $xml->items->client[0]->name . " | ";
echo $xml->items->client[0]->site[0]->name . " | ";
echo $xml->items->client[0]->site[0]->workstations[0]->workstation[0]->name . " | ";
echo $xml->items->client[0]->site[0]->workstations[0]->workstation[0]->failed_checks->check[0]->description . ": ";
echo $xml->items->client[0]->site[0]->workstations[0]->workstation[0]->failed_checks->check[0]->formatted_output . " ";
echo "<a href=\"https://www.-website-here.us/api/?apikey=apikeyhere&service=clear_check&checkid=";
echo $xml->items->client[0]->site[0]->workstations[0]->workstation[0]->failed_checks->check[0]->checkid . "\">";
echo $xml->items->client[0]->site[0]->workstations[0]->workstation[0]->failed_checks->check[0]->checkid;
echo "</a>"
?>
ASKER
Ray, I took what you had and was able to make things work.
Thanks!
Thanks!
<?php // RAY_temp_jonathanbyers.php
error_reporting(E_ALL);
echo '<pre>';
$url = 'http://filedb.experts-exchange.com/incoming/2013/01_w03/628988/alerts.xml';
$xml = file_get_contents($url);
$obj = SimpleXML_Load_String($xml);
// USE ITERATORS TO ACCESS THE OBJECT PROPERTIES
foreach ($obj->items->client as $c)
{
// var_dump($c);
echo PHP_EOL;
echo PHP_EOL . $c->clientid . " | ";
echo $c->name;
foreach ($c->site->servers->server as $w)
{
// var_dump($w);
echo PHP_EOL . $w->id . " | ";
echo $w->name . " | ";
foreach ($w->failed_checks->check as $k)
{
// var_dump($k);
echo $k->checkid . " | ";
echo $k->description;
//echo PHP_EOL;
}
}
}
echo PHP_EOL;
// VISUALIZE THE ENTIRE OBJECT
//var_dump($obj);
?>
Thanks for the points and thanks for using EE. Going forward, please feel free to share the points with others who offer good answers. The first post by Roads_Roads is also a valid way of looking at the issue.
And a small sidebar note. Lose the ?> close-PHP tag if at all possible. At least don't use it reflexively. There are a host of reasons to avoid it, and the Zend Coding Standard dis-recommends it. So if your script can work without it, make it standard practice to leave it out.
Best regards, ~Ray
And a small sidebar note. Lose the ?> close-PHP tag if at all possible. At least don't use it reflexively. There are a host of reasons to avoid it, and the Zend Coding Standard dis-recommends it. So if your script can work without it, make it standard practice to leave it out.
Best regards, ~Ray
http://php.net/manual/en/function.simplexml-load-file.php
Open in new window