[英]Compare two xml files, update/merge and save - php simplexml
我正在嘗試使用simplexml將一個xml文件與另一個項目進行比較和補充。 由於我是新手,因此嘗試為該問題找到合適的示例,但是經過幾天的搜索,幾乎沒有結果:只有點點滴滴。 初稿雖然不多,但我希望有人能給我一些指導。
xml源文件:catalog.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<item>
<name>King of Hearts</name>
<category>playing cards</category>
<class>Hearts</class>
<filter>King</filter>
</item>
<item>
<name>Ace of Spades</name>
<category>playing cards</category>
<class>Spades</class>
<filter>Ace</filter>
</item>
<item>
<name>Eight of Diamonds</name>
<category>playing cards</category>
<class>Diamonds</class>
<filter>Eight</filter>
</item>
</catalog>
目標xml文件:user.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<item>
<name>Ace of Spades</name>
<category>playing cards</category>
<class>Spades</class>
<filter>Ace</filter>
</item>
<item>
<name>Knight of Clubs</name>
<category>playing cards</category>
<class>Clubs</class>
<filter>Knight</filter>
</item>
</catalog>
比較/合並后更新的用戶文件:user-updated.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<item>
<name>Ace of Spades</name>
<category>playing cards</category>
<class>Spades</class>
<filter>Ace</filter>
</item>
<item>
<name>Knight of Clubs</name>
<category>playing cards</category>
<class>Clubs</class>
<filter>Knight</filter>
</item>
<item>
<name>King of Hearts</name>
<category>playing cards</category>
<class>Hearts</class>
<filter>King</filter>
</item>
<item>
<name>Eight of Diamonds</name>
<category>playing cards</category>
<class>Diamonds</class>
<filter>Eight</filter>
</item>
</catalog>
PHP代碼:merge.php
<?php
// catalog.xml in this example contains two extra items as user.xml
// these 'excess items' need to bee updated/copied from catalog.xml to user.xml
// the final order of items in user-updated.xml is of no concern:
// so it's probably easiest to just add them after the last existing item?
// 1. load data from catalog.xml & user.xml (which have the same root & structure)
$catalog = 'catalog.xml';
$user = 'user.xml';
$sourcexml = simplexml_load_file($catalog);
$targetxml = simplexml_load_file($user);
// 2. compare data and return nodes missing in user.xml
$result = array_diff($targetxml, $sourcexml);
// 3. add missing items (including children) to $targetxml
foreach($result->item as $item) {
$xml = $targetxml->addChild($item);
$xml->addChild('name', $item->name);
$xml->addChild('category', $item->category);
$xml->addChild('class', $item->class);
$xml->addChild('filter', $item->filter);
}
// 4. save updated $targetxml to user.xml
$targetxml->asXML('user-updated.xml');
?>
我為您的問題編寫了代碼。 您可以毫無問題地使用它。 :)
<?php
$catalog = 'catalog.xml';
$user = 'user.xml';
$sourcexml = simplexml_load_file($catalog);
$targetxml = simplexml_load_file($user);
$a1 = json_decode(json_encode($sourcexml),true);
$a2 = json_decode(json_encode($targetxml),true);
$items = array_map('unserialize',
array_unique(
array_map('serialize',
array_merge_recursive($a1['item'], $a2['item'])
)
)
);
$xml = new SimpleXMLElement('<?xml version="1.0"?><catalog></catalog>');
foreach ($items as $item){
$node = $xml->addChild('item');
$node->addChild('name', $item['name']);
$node->addChild('category', $item['category']);
$node->addChild('class', $item['class']);
$node->addChild('filter', $item['filter']);
}
echo $xml->asXML();
?>
我為您編寫了最好的代碼。 您可以合並2個或更多xml文件。
<?php
$files = array('catalog.xml', 'user.xml');
$xml = combine_xmls($files);
echo $xml->asXML();
function combine_xmls($files, $key='name'){
foreach ($files as $file){
$a = json_decode(json_encode(simplexml_load_file($file)),true);
$b[] = $a['item'];
}
$b = call_user_func_array('array_merge', $b);
foreach($b as $a) $items[$a[$key]] = $a;
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="ISO-8859-1"?><catalog></catalog>');
foreach ($items as $item ){
$node = $xml->addChild('item');
foreach ($item as $key=>$val) $node->addChild($key, $val);
}
return $xml;
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.