Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help With PHP's Object Model

Posted on 2012-03-13
9
Medium Priority
?
197 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:anshuma
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 17

Expert Comment

by:Chris Harte
ID: 37715412
object-model.txt is just the output of a print_r. Post the original, or give a url for the,  XML.
0
 

Author Comment

by:anshuma
ID: 37715443
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
 
LVL 14

Expert Comment

by:Scott Madeira
ID: 37715466
I would start here and learn how SimpleXML functions work in PHP.

http://www.php.net/manual/en/book.simplexml.php
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:Scott Madeira
ID: 37715498
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
 

Author Comment

by:anshuma
ID: 37715521
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
 

Author Comment

by:anshuma
ID: 37715635
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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 37715948
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
 
LVL 17

Expert Comment

by:Chris Harte
ID: 37720630
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37721182
0

Featured Post

Technology Partners: 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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

824 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