Solved

Quick php xpath question

Posted on 2009-07-01
7
228 Views
Last Modified: 2013-11-11
Hi all,

One hour has pass and I can't work out how to get the result of an xpath query!!!

With the below, how can I return one node's value? I mean, how would I say

for each result
echo show_me_the_streetname_value
next

???

Thanks,

Warren


<?php

$xmlDocument = new DOMDocument();

if ($xmlDocument->load('vw_WebsitePropertyData.xml')) {

	$xpath = new DOMXPath($xmlDocument);

	$nodeList = $xpath->query('/dataroot/vw_WebsitePropertyData[RentPerWeek>22]', $xmlDocument);

}

?>

Open in new window

0
Comment
Question by:warrenrapson
  • 4
  • 3
7 Comments
 
LVL 5

Expert Comment

by:mallcore
ID: 24760949
I would do the following:
<?php

$xml = simplexml_load_file("vw_WebsitePropertyData.xml");
 

$results = $xml->xpath("/dataroot/vw_WebsitePropertyData[RentPerWeek>22]'");
 

foreach ($results as $result)

{

   print_r($result);

}
 

?>

Open in new window

0
 

Author Comment

by:warrenrapson
ID: 24761001
Great!

So how would I get a named value from that array object?

Eg.

echo 'Street Name: ' . ?????????????
0
 
LVL 5

Expert Comment

by:mallcore
ID: 24761024
echo 'Street Name:' . $result[0]

If you have multiple values put it into a for loop like this:
for($i = 0; $i >= count($result) ; $i++)

{

  echo 'Street Name: ' . $result[$i];

}
 

Should work.

Open in new window

0
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.

 
LVL 5

Expert Comment

by:mallcore
ID: 24761029
Ops made a mistake, sorry.


for($i = 0; $i <= count($result) ; $i++)

{

  echo 'Street Name: ' . $result[$i];

}

Open in new window

0
 

Author Comment

by:warrenrapson
ID: 24761101
I tried that first, and I could see how that would work, but the array contains the SimpleXMLElement object. Below is part of the print_r output

SimpleXMLElement Object ( [ID] => 2 [Contact] => Paul Bradford [Street] => 10 Jeffson Street..............

So I am still lost as to how to pluck the 'street' value from this result set.
0
 
LVL 5

Accepted Solution

by:
mallcore earned 500 total points
ID: 24761129
Try $result->Street;
0
 

Author Comment

by:warrenrapson
ID: 24761232
Brilliant!!

Thanks for your help.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

21 Experts available now in Live!

Get 1:1 Help Now