Solved

Parsing Nested XML With PHP

Posted on 2013-01-18
5
317 Views
Last Modified: 2013-01-18
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
0
Comment
Question by:jonathanbyers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38794183
SimpleXML is your friend:

http://php.net/manual/en/function.simplexml-load-file.php

<?php
error_reporting(E_ALL);

$xml = simplexml_load_file("alerts.xml");

echo '<pre>';
print_r($xml);

echo $xml->items->client[0]->clientid;

Open in new window

0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38794244
I find the combination of the <pre> tag with the foreach() iterator and var_dump() makes this work much easier.
http://www.laprbass.com/RAY_temp_jonathanbyers.php

<?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 . $c->clientid;
    foreach ($c->site->workstations->workstation as $w)
    {
        // var_dump($w);
        echo PHP_EOL . '  ' .$w->id;
        foreach ($w->failed_checks->check as $k)
        {
            // var_dump($k);
            echo PHP_EOL . '    ' . $k->checkid;
            echo PHP_EOL . '    ' . $k->check_type;
            echo PHP_EOL;
        }
    }
}
echo PHP_EOL;

// VISUALIZE THE ENTIRE OBJECT
var_dump($obj);

Open in new window

0
 

Author Comment

by:jonathanbyers
ID: 38794318
@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()?  

<?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>"

?>

Open in new window

0
 

Author Comment

by:jonathanbyers
ID: 38794412
Ray, I took what you had and was able to make things work.

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);
?>

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38795193
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question