如何通过php比较相同atribute名称的两个XML文件

如何通过php比较相同atribute名称的两个XML文件,php,xml,Php,Xml,我有两个XML文件string1.XML和string2.XML每个文件都包含更多的行代码。我想根据相同的属性名比较两个XML文件 我想对下面的表达式这样的所有行进行迭代循环 if (attribute name cancel in string1.xml also found in string2.xml) { echo '<string name="attrName from string1.xml">Atribute Value from string2.xml<

我有两个XML文件
string1.XML
string2.XML
每个文件都包含更多的行代码。我想根据相同的属性名比较两个XML文件

我想对下面的表达式这样的所有行进行迭代循环

if (attribute name cancel in string1.xml also found in string2.xml) {
    echo '<string name="attrName from string1.xml">Atribute Value from string2.xml</string>'
} else {
    echo '<string name="attrName from string1.xml">Atribute Value from string1.xml</string>'
}
if(string1.xml中的属性名cancel也可以在string2.xml中找到){
回显“string2.xml中的Atribute值”
}否则{
回显“string1.xml中的Atribute值”
}
string1.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="cancel">Cancel</string>
    <string name="copy">Copy</string>
    <string name="copyUrl">Copy URL</string>
</resources>

取消
复制
复制URL
string2.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="cancel">US</string>
    <string name="paste">Italy</string>
    <string name="copyUrl">Germany</string>
</resources>

美国
意大利
德国

您可以这样做:

<?php

$resource1 = new SimpleXMLElement($xml);
$resource2 = new SimpleXMLElement($xml);

$array1 = $resource1->string;
$array2 = $resource2->string;

$differences = array_diff($array1, $array2);

钥匙是否始终匹配?你试过使用吗?结构始终是单层还是多层?检查此处查看如何检查属性是否可用@mkaatman是的结构始终是单层我尝试了单个XML文件的SimpleXML,但我尝试了与失败的文件进行比较我无法创建正确的foreach循环来迭代这两行中的所有行files@mkaatman2文件的属性名称略有不同,但属性值完全不同