SAFEEN 1990
SAFEEN 1990

Reputation: 310

How to compare Two XML files for same atribute names by php

I have two XML files string1.xml and string2.xml every file contain the more line codes. I want to Compare Two XML files according to same attr names

I want iterate loop for all lines like this expression below

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>

Upvotes: 0

Views: 153

Answers (1)

Matt
Matt

Reputation: 5428

You could do something like this:

<?php

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

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

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

Or maybe you could loop through the first one looking for items in the 2nd.

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

Upvotes: 1

Related Questions