Jump to content

Checking whether the searching attribute exists in a XML file using PHP


raaks

Recommended Posts

I have come across couple of similar answers for my question in this form, but could not solve my exact problem. Therefore I am posting this here:

 

I have an xml file as shown below:

 

<?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>

I ask the user to input his user_id and post this user_id and perform a check on the entire xml file to check whether the entered user_id exists in the xml file or not. If exists then I go further if not echo a error message. Can anybody pull me out of this?

 

Thanks

What have you tried?

 

Are you using simplexml_load_file (or simplexml_load_string)

 

Here's an example from php.net

// The file test.xml contains an XML document with a root element
// and at least an element /[root]/title.

if (file_exists('test.xml')) {
    $xml = simplexml_load_file('test.xml');

    print_r($xml);
} else {
    exit('Failed to open test.xml.');
}

 

outputs:

 

SimpleXMLElement Object
(
  [title] => Example Title
)

 

from an xml file that had xml along these lines:

<?xml version="1.0" encoding="UTF-8"?>
    <docroot>
        <title>Example Title</title>
    </docroot>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.