Solved

Help With PHP's Object Model

Posted on 2012-03-13
9
186 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
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.
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 …

760 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

17 Experts available now in Live!

Get 1:1 Help Now