Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Formatting XML results

Posted on 2007-03-30
8
166 Views
Last Modified: 2010-05-18
I'm using a PHP and XML script to connect to a remote server and retrive data. I have most of it setup, but I'm trying to format the data that gets returned.

For example, if I use

var_dump($response);

to dump my results, I get something like the following:

object(DomainInfDataResponse)#15 (12) { ["name:private"]=>  string(14) "domainname.pl" ["status:private"]=>  string(6) "Active"  ["nss:private"]=>  array(2) { [0]=>  object(DomainNs)#20 (2) { ["name:private"]=>  string(15) "ns1.domainname.pl" ["ip:private"]=>  string(0) "" } [1]=>  object(DomainNs)#22 (2) { ["name:private"]=>  string(15) "ns2.domainname.pl" ["ip:private"]=>  string(0) "" } } ["holder:private"]=>  object(DomainHolder)#23 (4) { ["class:private"]=>  string(11) "Company" ["remark:private"]=>  string(21) "changed admin by name" } ["admins:private"]=>  array(1) { [0]=>  string(10) "NOC-453" } ["techs:private"]=>  array(1) { [0]=>  string(10) "NOC-453" } ["account:private"]=>  string(8) "00000161" ["regDate:private"]=>  string(10) "2001-06-27" ["renDate:private"]=>  string(10) "2007-06-27" ["result:private"]=>  object(Result)#6 (5) { ["code:private"]=>  string(4) "1000"  } ["type:protected"]=>  int(2) }

To retrive a single value from the response, I thought all I needed was:
$domain = $response=>name;

but that doesn't work.

How would I call a single variable?
0
Comment
Question by:thunder99
  • 3
  • 3
  • 2
8 Comments
 
LVL 4

Expert Comment

by:secondv
ID: 18823137
PHP5?

Could you provide some code, or give the output from:

print_r($response);
0
 

Author Comment

by:thunder99
ID: 18823363
Hi Secondv

Yes, it's php5.

print_r($response); gives:

DomainInfDataResponse Object ( [name:private] => domainname.pl [status:private] => Active [billingStatus:private] => Billable [nss:private] => Array ( [0] => DomainNs Object ( [name:private] => ns1.domainname.pl [ip:private] => ) [1] => DomainNs Object ( [name:private] => ns2.domainname.pl [ip:private] => ) ) [holder:private] => DomainHolder Object ( [class:private] => Company [remark:private] => changed admin by name ) [admins:private] => Array ( [0] => NOC-453 ) [techs:private] => Array ( [0] => NOC-453 ) [account:private] => 00000161 [regDate:private] => 2001-06-27 [renDate:private] => 2007-06-27 [result:private] => Result Object ( [code:private] => 1000 [msg:private] => Command completed successfully [value:private] => [reasonCode:private] => [reasonMsg:private] => ) [type:protected] => 2 )["type:protected"]=>  int(2) }

I can't really post the code as it's coming from lots of config files that are been called once.

The XML command that's been created looks like:

$command = new DomainInfoCommand("domainname.pl");
$response = CommandProcessor::process($command);
$result = $response->getResult();

//Test it//
var_dump($response);
print_r($response);

I actually had it working before, and then didn't save some changes and can't remember how I did it.


0
 
LVL 4

Accepted Solution

by:
secondv earned 250 total points
ID: 18823570
To get a value:

echo $response->name;

However, if the variables are private "[name:private]" you won't be able to access it that way.

(Fatal error: Cannot access private property DomainInfDataResponse::$name in xxxx on line xx)

What you could do, is add a function inside the class that will return a given value.. such as:

public function get_value($var)
{
      return $this->$var;
}

$response->get_value('name');


0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 2

Expert Comment

by:wakemup
ID: 18824661
You can also use a generic __get($varname) function so:

function __get($varname) {

}
0
 
LVL 2

Expert Comment

by:wakemup
ID: 18824672
Sorry:

function __get($varname) {
  return $this->$varname;
}

This should work in php 5.0+
0
 
LVL 2

Assisted Solution

by:wakemup
wakemup earned 250 total points
ID: 18824692

You can then just call '$this->Fred' and the __get function gets called automatically in the background and the value is returned without you having to do a get_fred, get_john function or a get($varname) and explicitly saying which variable you want. PHP 5 is cool like this.

There is also a __set($varname, $varvalue) which does the same the other way. Those are two underscores by the way.
0
 

Author Comment

by:thunder99
ID: 18825772
Can't get any of those methods to work. I'll go through some of the other files that are been called to see if it gives any hints as to what I need.
0
 

Author Comment

by:thunder99
ID: 19125851
Sorry for the delay in getting back to this question. Both of the solutions offered by secondv and wakemup work, so I'm splitting the points.

Again apoligies for leaving it open so long.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
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 …

839 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