2017-03-27 7 views
0

私は2つのXMLファイルstring1.xmlstring2.xmlすべてのファイルには、より多くの行コードが含まれています。私はどのように比較する同じ属性名の2つのXMLファイルはPHPで

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

以下、この式のようにすべての行のためのループを繰り返すたく同じattrの名前

に応じて2つの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

キーは常に一致しますか?あなたは[SimpleXML](http://php.net/manual/en/book.simplexml.php)を使ってみましたか?構造は常に単一の層ですか、それとも複数の層が深いことができますか? – mkaatman

+0

check http://stackoverflow.com/questions/699821/php-check-if-xml-node-exists-with-attribute – webDev

+0

属性を確認する方法はこちらをご覧くださいhttp://stackoverflow.com/questions/10909372/check-if-object-attribute-is-set-simplexml – webDev

答えて

1

あなたは次のようにすることができます:

<?php 

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

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

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

また、最初のものをループして2番目のアイテムを探すこともできます。

foreach($array1 as $key => $value) { if(array_key_exists($key, $array2) && $array1[$key] === $array2[$key]) { echo "$key is the same."; } }

関連する問題