0

I have been playing with XML and PHP, I have googled to get the requirement which I am working on but still Im not getting the things the way I wanted.

I have got a XML file with nested variables, I need to parse the variables such as user_id, name and store it in an array, so that I need to use them to change the details in another config file.

For ex: if the user is logged in, using the user_id, i need to parse the all the data of the particular user from the XML file (where the details of the various users are stored).

Until now I could only parse the data and echo all of it but could not find a way to store this parsed information.

This is my test.xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<document>
    <user>
        <user_id>0121</user_id>
        <name>Tim</name>
        <file>0121.file</file>
    </user>

    <user>
        <user_id>0178</user_id>
        <name>Henry</name>
        <file>0178.file</file>
    </user>

    <user>
        <user_id>0786</user_id>
        <name>Martin</name>
        <file>0786.file</file>
    </user>

    <user>
        <user_id>1239</user_id>
        <name>Jan</name>
        <file>1239.file</file>
    </user>
</document>

Any valuable information would be greatly helpful

Thanks Raaks

125369
  • 3,547
  • 20
  • 52
  • 70
  • @Gordon, thanks for your reply but I am looking for a way to store tha parsed information into an array or variable so that I can use these information for updating another file not the same XML file or another XML file. Hope you go it. – 125369 Nov 22 '11 at 12:04

1 Answers1

0

Use SimpleXML.

$xml = simplexml_load_file("test.xml");
$user = $xml->xpath("user[user_id = $my_user_id]");

$name = $user[0]->name;
$file = $user[0]->file;
socha23
  • 10,171
  • 2
  • 28
  • 25
  • Hi Socha, i followed your tip and im getting this error "Warning: SimpleXMLElement::xpath() [simplexmlelement.xpath]: Invalid expression" Any idea why this is popping up – 125369 Nov 22 '11 at 13:01
  • yes, it should be a single =, not ==. Also, $user lacks index in later calls. I updated my answer, it should work now. – socha23 Nov 22 '11 at 13:18
  • Hi Socha, thanks a lot for the quick help. Need to say for the last half an hour was struggling to find out where the error is lying. Any way thanks for updating your code. Now everything works good – 125369 Nov 22 '11 at 13:28