Solved

Help With PHP's Object Model

Posted on 2012-03-13
9
189 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 16

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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 108

Accepted Solution

by:
Ray Paseur earned 500 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 16

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 108

Expert Comment

by:Ray Paseur
ID: 37721182
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now