Solved

Formatting XML results

Posted on 2007-03-30
8
163 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
 
LVL 2

Expert Comment

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

function __get($varname) {

}
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

863 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

24 Experts available now in Live!

Get 1:1 Help Now