Reputation: 161
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".
Upvotes: 0
Views: 1700
Reputation: 4874
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";
}
Upvotes: 1