tags:

views:

21

answers:

1

I am trying to access certain pieces of data from an xml file, here is the problem.

###XML FILE
<products>
    <product>
        ....
        ....
    </product>
    <product>
        ....
        ....
    </product>
    etc...
</products>

I know that the piece of data I need is in ($products->product->myProdNode) I have this mapping (and many others) stored in my database as a string e.g.'product->prodCode' or 'product->dedscriptions->short_desc' How can I access this data by using the strings stored in my database.

Thanks for you help in advance!

+2  A: 

I think if you replace your -> with forward slash (/), they effectively become Xpath and you can query Node contents like that.

E.g

'product->dedscriptions->short_desc' should be mapped to
'product/dedscriptions/short_desc'

Please read more on Xpath here

E.g. In C#

XmlNode.SelectSingleNode("product/dedscriptions/short_desc").InnerText will get the short description text

In php

$result = $record->xpath('descriptions/short_description');

while(list( , $node) = each($result)) { echo 'Results is: ',$node,"\n"; }

J Angwenyi
How would i get the data with this method?
Lizard
$record->xpath('descriptions/short_description'); - I want just the value not the SimpleXMLElement Object
Lizard
Am not sure about the properties and methods of SimpleXMLElement Object, but you should access the inner text or value
J Angwenyi