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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.