我有两个XML文件string1.xml
和string2.xml
,每个文件都包含更多的行代码。我想根据相同的attr名称比较两个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>'
}
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>
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>
答案 0 :(得分:1)
你可以这样做:
<?php
$resource1 = new SimpleXMLElement($xml);
$resource2 = new SimpleXMLElement($xml);
$array1 = $resource1->string;
$array2 = $resource2->string;
$differences = array_diff($array1, $array2);
或许你可以循环搜索第一个中的第一个项目。
foreach($array1 as $key => $value) {
if(array_key_exists($key, $array2) && $array1[$key] === $array2[$key] ) {
echo "$key is the same.";
}
}