如何使用 simplexml_load_string 将多个 XML 文件加载到一个字符串中?

发布于 2024-12-12 04:43:27 字数 621 浏览 0 评论 0原文

我想使用 simplexml_load_string 函数将三个 xml 文件加载到我的 php 文件中。

我怎样才能实现这个目标?

@Jan-Henk:

这是我在评论中写的内容:

这就是我目前所拥有的,它搜索员工 Jane 的一个 xml 文件:

$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 />";

三个 xml 文件会是什么样子。然后不要像上面那样搜索员工 Jane。它应该搜索这三个文件是否有重复项。因此,如果一名员工在三个文件中的任何一个中被列出两次。它应该返回:“找到简”。

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".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉徒眸意 2024-12-19 04:43:27

只需分别加载所有 3 个文件:

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

如果您确实想使用 simplexml_load_string 而不是 simplexml_load_file,您可以执行以下操作:

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

回答问题中的编辑,该编辑仅适用于 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";
}

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";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文