Forum Moderators: coopster

Message Too Old, No Replies

use php to get specific xml node/data

         

surrealillusions

6:38 pm on Oct 4, 2010 (gmt 0)

10+ Year Member



Hi all,

Is there a way to get the data of a specific block in XML?

Say theres this:

<data>
<title>set title</title>
<date>october 12th 2010</date>
<item>some stuff</item>
</data>

So, I want to display all inside the <data> for titles that match a variable.

I have seen stuff like xpath, but that doesn't look straight forward.

surrealillusions

11:34 am on Oct 5, 2010 (gmt 0)

10+ Year Member



SO, I have this, based on a bit of code from php.net:


$sxe = simplexml_load_file("stuff.xml");
foreach($sxe->xpath('//data') as $item) {

$row = simplexml_load_string($item->asXML());
$v = $row->xpath('//title[ =".$pageitem."]');

if($v[0]){
print $item->title;
print $item->sizes;
print $item->date;
}

}


However, this line is giving problems:

$v = $row->xpath('//title[ =".$pageitem."]');

It basically wont work with the $pageitem variable in there. If I put just text in, then it works. But I need it to be a variable.

Any work arounds?

enigma1

3:03 pm on Oct 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if you want to pass a variable to that function you should rewrite the code like:

$v = $row->xpath('//title[ ="'.$pageitem.'"]');

surrealillusions

5:29 pm on Oct 5, 2010 (gmt 0)

10+ Year Member



I get this error:

Warning: SimpleXMLElement::xpath() [simplexmlelement.xpath]: Invalid expression

On that line: $v = $row->xpath('//title[ ="'.$pageitem.'"]');

enigma1

6:05 pm on Oct 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well it suppose to be a string like

$row->xpath('/a/b/c');

So I don't know what the pageitem var is in your case and if you meant to set it up like:

$row->xpath('/' . $pageitem);

surrealillusions

6:55 pm on Oct 5, 2010 (gmt 0)

10+ Year Member



$v = $row->xpath('//' . $pageitem);

doesn't return anything. It gets rid of the error, but nothing is outputted.

Basically, $pageitem will match one of the <name></name> elements within the <data></data> section in the XML file.

Anyango

6:03 am on Oct 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I usually convert xml into array and then go from there. just a preference. you might want to look at that

enigma1

8:53 am on Oct 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you setup the pageitem to be a string field like 'title' or 'item' without any other special characters because I don't know the exact string you are using if I try it with

$pageitem = 'some stuff';
$v = $row->xpath('//item[.="' . $pageitem . '"]');

Then it will return the specific entry here. The page item in this case must match exactly the tag content.

surrealillusions

1:19 pm on Oct 7, 2010 (gmt 0)

10+ Year Member



Have this now which works:

$sxe = simplexml_load_file("stuff.xml");
$results = $sxe->xpath("//data/item[.='$pageitem']/.."); // returns an array of objects