Solved

Help With PHP's Object Model

Posted on 2012-03-13
9
191 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 109

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 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 109

Expert Comment

by:Ray Paseur
ID: 37721182
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

830 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