Help With PHP's Object Model

Hi all,

I am a newbie to php and I am struggling with the object model. I am executing a web-services url and getting the xml.

I need to parse it and show the output in a standard format

The object model and expected output are attached in text. Will appreciate if you can help me with traversing it and then showing sample code for generating the expected output.

thanks
-anshu
expected-output.txt
Object-Model.txt
anshumaEngineeringAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris HarteThaumaturgeCommented:
object-model.txt is just the output of a print_r. Post the original, or give a url for the,  XML.
0
anshumaEngineeringAuthor Commented:
Hi Munterman,

I cannot provide the xml, what I have posted is the output of var_dump in the following code.

$reportxmldata=file_get_contents($reportaskxmlurl);
$reportxml = SimpleXML_Load_String($reportxmldata);
echo "<pre>";
var_dump($reportxml);
echo "<br>";


thanks
-anshu
0
Scott MadeiraCommented:
I would start here and learn how SimpleXML functions work in PHP.

http://www.php.net/manual/en/book.simplexml.php
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Scott MadeiraCommented:
Two things...

1. If this is a homework assignment for a class, EE is not permitted to give you the answers.  We can give you clues and help you get to the answer.

2. Can you post the contents of the file referenced in this statement:  

$reportxmldata=file_get_contents($reportaskxmlurl);

that should be your source XML. You can't do var_dumps of simpleXML data.  As you can see, it isn't very useful.
0
anshumaEngineeringAuthor Commented:
this is not homeowrk assignment :-) the output is coming from a BI Tool.

So instead of vardump what should I give you and how.
0
anshumaEngineeringAuthor Commented:
Here's the output of that

thanks
-anshu

<taskResponse statusCode="200">
<report_data_list scp="1" crt="1331655743000"><report_data locc="US" id="D1AE56B011D5C4D04C200E8820504F4F" locid="1033" name="Call Center Timeliness" locl="en" istab="1"><prs><pr v="0" n="LongNames" psn="Template Formatting"></pr><pr v="" n="VisProps" psn="Visualization"></pr></prs><styles><style id="c17"><prs n="FormattingNumber"><pr v="4" n="Category"></pr><pr v="2" n="DecimalPlaces"></pr><pr v="-1" n="ThousandSeparator"></pr><pr v="$" n="CurrencySymbol"></pr><pr v="0" n="CurrencyPosition"></pr><pr v="0.00%" n="Format"></pr><pr v="1" n="NegativeNumbers"></pr></prs></style><style id="c16"><prs n="FormattingNumber"><pr v="4" n="Category"></pr><pr v="2" n="DecimalPlaces"></pr><pr v="-1" n="ThousandSeparator"></pr><pr v="$" n="CurrencySymbol"></pr><pr v="0" n="CurrencyPosition"></pr><pr v="0.00%" n="Format"></pr><pr v="1" n="NegativeNumbers"></pr></prs></style></styles><objects><attribute rfd="0" id="8D679D3511D3E4981000E787EC6DE8A4" name="Call Center" type="12"><form rfd="1" id="CCFBE2A5EADB4F50941FB879CCF1721C" base_form_type="3" name="DESC" type="21"></form></attribute><metric rfd="2" id="4C051EAF11D3E877C000B3B2D86C964F" name="Timeliness" type="4"><format cat="4" cp="0" ts="-1" nn="1" dp="2" cs="$">0.00%</format></metric></objects><template><axis id="1"><unit rfd="0" unit_type="1"></unit></axis><axis id="2"><unit unit_type="3"><oi rfd="2"></oi></unit></axis><axis id="3"></axis></template><raw_data><headers><oi rfd="0"></oi><oi rfd="2"></oi></headers><rows cn="15">
 
<r fr="1"><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:1">Atlanta</v><v stid="c16">26.92%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:2">San Diego</v><v stid="c17">27.20%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:4">San Francisco</v><v stid="c16">26.74%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:5">Washington, DC</v><v stid="c17">26.40%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:6">Salt Lake City</v><v stid="c16">27.30%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:7">Miami</v><v stid="c17">27.03%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:8">Milwaukee</v><v stid="c16">27.05%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:9">New Orleans</v><v stid="c17">27.01%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:10">Seattle</v><v stid="c16">26.73%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:11">Boston</v><v stid="c17">26.52%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:12">New York</v><v stid="c16">27.50%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:14">Fargo</v><v stid="c17">27.22%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:15">Memphis</v><v stid="c16">26.69%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:17">Charleston</v><v stid="c17">27.01%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:18">Web</v><v stid="c16">26.94%</v></r></rows></raw_data></report_data></report_data_list>
</taskResponse>
0
Ray PaseurCommented:
See http://www.laprbass.com/RAY_temp_anshuma.php
Outputs:
Call Center|Timeliness
Atlanta|26.92%
San Diego|27.20%
San Francisco|26.74%
Washington, DC|26.40%
Salt Lake City|27.30%
Miami|27.03%
Milwaukee|27.05%
New Orleans|27.01%
Seattle|26.73%
Boston|26.52%
New York|27.50%
Fargo|27.22%
Memphis|26.69%
Charleston|27.01%
Web|26.94%
<?php // RAY_temp_anshuma.php
error_reporting(E_ALL);
echo "<pre>";

$xml = <<<XML
<taskResponse statusCode="200">
<report_data_list scp="1" crt="1331655743000"><report_data locc="US" id="D1AE56B011D5C4D04C200E8820504F4F" locid="1033" name="Call Center Timeliness" locl="en" istab="1"><prs><pr v="0" n="LongNames" psn="Template Formatting"></pr><pr v="" n="VisProps" psn="Visualization"></pr></prs><styles><style id="c17"><prs n="FormattingNumber"><pr v="4" n="Category"></pr><pr v="2" n="DecimalPlaces"></pr><pr v="-1" n="ThousandSeparator"></pr><pr v="$" n="CurrencySymbol"></pr><pr v="0" n="CurrencyPosition"></pr><pr v="0.00%" n="Format"></pr><pr v="1" n="NegativeNumbers"></pr></prs></style><style id="c16"><prs n="FormattingNumber"><pr v="4" n="Category"></pr><pr v="2" n="DecimalPlaces"></pr><pr v="-1" n="ThousandSeparator"></pr><pr v="$" n="CurrencySymbol"></pr><pr v="0" n="CurrencyPosition"></pr><pr v="0.00%" n="Format"></pr><pr v="1" n="NegativeNumbers"></pr></prs></style></styles><objects><attribute rfd="0" id="8D679D3511D3E4981000E787EC6DE8A4" name="Call Center" type="12"><form rfd="1" id="CCFBE2A5EADB4F50941FB879CCF1721C" base_form_type="3" name="DESC" type="21"></form></attribute><metric rfd="2" id="4C051EAF11D3E877C000B3B2D86C964F" name="Timeliness" type="4"><format cat="4" cp="0" ts="-1" nn="1" dp="2" cs="$">0.00%</format></metric></objects><template><axis id="1"><unit rfd="0" unit_type="1"></unit></axis><axis id="2"><unit unit_type="3"><oi rfd="2"></oi></unit></axis><axis id="3"></axis></template><raw_data><headers><oi rfd="0"></oi><oi rfd="2"></oi></headers><rows cn="15">

<r fr="1"><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:1">Atlanta</v><v stid="c16">26.92%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:2">San Diego</v><v stid="c17">27.20%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:4">San Francisco</v><v stid="c16">26.74%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:5">Washington, DC</v><v stid="c17">26.40%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:6">Salt Lake City</v><v stid="c16">27.30%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:7">Miami</v><v stid="c17">27.03%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:8">Milwaukee</v><v stid="c16">27.05%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:9">New Orleans</v><v stid="c17">27.01%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:10">Seattle</v><v stid="c16">26.73%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:11">Boston</v><v stid="c17">26.52%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:12">New York</v><v stid="c16">27.50%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:14">Fargo</v><v stid="c17">27.22%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:15">Memphis</v><v stid="c16">26.69%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:17">Charleston</v><v stid="c17">27.01%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:18">Web</v><v stid="c16">26.94%</v></r></rows></raw_data></report_data></report_data_list>
</taskResponse>
XML;

// A DELIMITER
$dlm = '|';

// MAKE AN OUTPUT BUFFER
$out = NULL;

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO VISUALIZE THE OBJECT
// print_r($obj);

// USE AN ITERATOR TO ACCESS SOME OF THE PROPERTIES
foreach ($obj->report_data_list->report_data->objects as $object)
{
    $out
    .= (string)$object->attribute["name"]
    . $dlm
    . (string)$object->metric["name"]
    . PHP_EOL
    ;
}

// USE AN ITERATOR TO ACCESS MORE OF THE PROPERTIES
foreach ($obj->report_data_list->report_data->raw_data->rows as $row)
{
    foreach ($row->r as $robj)
    {
        $out
        .= (string)$robj->v[0]
        . $dlm
        . (string)$robj->v[1]
        . PHP_EOL
        ;
    }
}

// SHOW THE WORK PRODUCT
$out = trim($out);
print_r($out);

Open in new window

HTH, ~Ray
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris HarteThaumaturgeCommented:
Read it as a simplexml and output the desired values with a foreach

<?php

$url = <<<xml
<taskResponse statusCode="200">
<report_data_list scp="1" crt="1331655743000"><report_data locc="US" id="D1AE56B011D5C4D04C200E8820504F4F" locid="1033" name="Call Center Timeliness" locl="en" istab="1"><prs><pr v="0" n="LongNames" psn="Template Formatting"></pr><pr v="" n="VisProps" psn="Visualization"></pr></prs><styles><style id="c17"><prs n="FormattingNumber"><pr v="4" n="Category"></pr><pr v="2" n="DecimalPlaces"></pr><pr v="-1" n="ThousandSeparator"></pr><pr v="$" n="CurrencySymbol"></pr><pr v="0" n="CurrencyPosition"></pr><pr v="0.00%" n="Format"></pr><pr v="1" n="NegativeNumbers"></pr></prs></style><style id="c16"><prs n="FormattingNumber"><pr v="4" n="Category"></pr><pr v="2" n="DecimalPlaces"></pr><pr v="-1" n="ThousandSeparator"></pr><pr v="$" n="CurrencySymbol"></pr><pr v="0" n="CurrencyPosition"></pr><pr v="0.00%" n="Format"></pr><pr v="1" n="NegativeNumbers"></pr></prs></style></styles><objects><attribute rfd="0" id="8D679D3511D3E4981000E787EC6DE8A4" name="Call Center" type="12"><form rfd="1" id="CCFBE2A5EADB4F50941FB879CCF1721C" base_form_type="3" name="DESC" type="21"></form></attribute><metric rfd="2" id="4C051EAF11D3E877C000B3B2D86C964F" name="Timeliness" type="4"><format cat="4" cp="0" ts="-1" nn="1" dp="2" cs="$">0.00%</format></metric></objects><template><axis id="1"><unit rfd="0" unit_type="1"></unit></axis><axis id="2"><unit unit_type="3"><oi rfd="2"></oi></unit></axis><axis id="3"></axis></template><raw_data><headers><oi rfd="0"></oi><oi rfd="2"></oi></headers><rows cn="15">
 
<r fr="1"><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:1">Atlanta</v><v stid="c16">26.92%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:2">San Diego</v><v stid="c17">27.20%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:4">San Francisco</v><v stid="c16">26.74%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:5">Washington, DC</v><v stid="c17">26.40%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:6">Salt Lake City</v><v stid="c16">27.30%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:7">Miami</v><v stid="c17">27.03%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:8">Milwaukee</v><v stid="c16">27.05%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:9">New Orleans</v><v stid="c17">27.01%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:10">Seattle</v><v stid="c16">26.73%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:11">Boston</v><v stid="c17">26.52%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:12">New York</v><v stid="c16">27.50%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:14">Fargo</v><v stid="c17">27.22%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:15">Memphis</v><v stid="c16">26.69%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:17">Charleston</v><v stid="c17">27.01%</v></r>
<r><v id="BB:8D679D3511D3E4981000E787EC6DE8A4:1:1:0:3:18">Web</v><v stid="c16">26.94%</v></r></rows></raw_data></report_data></report_data_list>
</taskResponse>
xml;


$contents = simplexml_load_string($url);


foreach ($contents->report_data_list->report_data->raw_data->rows->r as $xml)
{
    echo $xml->v[0]." | ".$xml->v[1] . "<br />";
}



?>

Open in new window

0
Ray PaseurCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.