Solved

Quick php xpath question

Posted on 2009-07-01
7
240 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
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 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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