0

I have three xml files that I want to load into my php file with the simplexml_load_string function.

How can I achieve this?

@Jan-Henk:

Here is what I wrote on the comments:

This is what I have at the moment , it searches one xml file for the employee Jane:

$result   = simplexml_load_file('employees1.xml');
echo "<strong>Matching employees with name 'Jane'</strong><br />";
$employees = $result->xpath('/employees/employee[name="Jane"]');

foreach($employees as $employee) {
    echo "Found {$employee->name}<br />";
}
echo "<br />";

How would this look like with three xml files. And then instead of searching for employee Jane like above. It should search the three files for duplicates. So if an employee is listed two times in either of the three files. It should return: "Found Jane".

1
  • Try casting them to string, a then merge arrays. Commented Oct 25, 2011 at 21:10

1 Answer 1

1

Just load all 3 files separately:

$xmlOne   = simplexml_load_file('path/to/file1.xml');
$xmlTwo   = simplexml_load_file('path/to/file2.xml');
$xmlThree = simplexml_load_file('path/to/file3.xml');

If you really want to use simplexml_load_string instead of simplexml_load_file you can do:

$xmlOne = simplexml_load_string(file_get_contents('path/to/file1.xml'));

Answer for the edit in your question, which only works for the name Jane:

$files = array('employees1.xml', 'employees2.xml', 'employees3.xml');
$xpathQuery = '/employees/employee[name="Jane"]';
$count = 0;

foreach ($files as $file) {
    $xml = simplexml_load_file($file);
    $result = $xml->xpath($xpathQuery);

    if (count($result) > 0) {
        $count++;
    }
}

if ($count > 1) {
    echo "Duplicates for Jane";
} else {
    echo "No duplicates for Jane";
}
Sign up to request clarification or add additional context in comments.

5 Comments

Does it matter which one I use, simplexml_load_string or simplexml_load_file? The php file should load three xml files. And then im using this guide to filter the results on ALL three files.
simplexml_load_file does everything at once, so that is just easier. As for processing all three files, you can simply perform the code you want to run against all three variables.
"Answer for the edit in your question, which only works for the name Jane" - How can I make it work with all duplicated. It should search the three files, if any duplicates are found it should display them. Duplicates found....
Then you should first extract all the names from the three XML files, and then use the code above in a loop to check for each name. But I am not going to write all that code for you, you should put in some effort yourself.
Yes I will, thanks for taking your time and helping me. I'm new at this. And I don't want you to write all that code. But can you just point me in the right direction, on where or what I should search to learn to extract all the names from the XML files. So I can use your code above. Thanks once again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.