Home > Backend Development > PHP Tutorial > 【100分求助】请问PHP中如何判断两个XML节点是否相同

【100分求助】请问PHP中如何判断两个XML节点是否相同

WBOY
Release: 2016-06-23 14:00:59
Original
1532 people have browsed it

请问各路大神,在XML节点 层次结构不确定的情况下,如何判断两个XML节点是否相同?
这里我们约定当节点存在子节点时,子节点的排列顺序不影响XML节点的判断。

例如有以下两个XML节点:
节点1:

<a>    <b><d>1</d></b>    <c>2</c></a>
Copy after login

节点2:
<a>    <c>2</c>    <b><d>1</d></b></a>
Copy after login

在我们现在的逻辑中,认为节点1和节点2是 相同的两个节点。

请问在PHP中有没有什么办法可以判断这两个节点是相同的呢?

谢谢!


回复讨论(解决方案)

$s =<<< XML<a>    <b><d>1</d></b>    <c>2</c></a><a>    <c>2</c>    <b><d>1</d></b></a>XML;$xml = simplexml_load_string($s);$t = $xml->xpath('///a');$t = array_map('objectToarray', $t);//print_r($t);var_dump($t[0] == $t[1]);function objectToArray($d) {   if (is_object($d)) {     $d = get_object_vars($d);   }  if (is_array($d)) {     return array_map(__FUNCTION__, $d);   } else {     return $d;   } }
Copy after login
Copy after login
bool(true)

T T 多谢版主!一个很好的思路~

$s =<<< XML<a>    <b><d>1</d></b>    <c>2</c></a><a>    <c>2</c>    <b><d>1</d></b></a>XML;$xml = simplexml_load_string($s);$t = $xml->xpath('///a');$t = array_map('objectToarray', $t);//print_r($t);var_dump($t[0] == $t[1]);function objectToArray($d) {   if (is_object($d)) {     $d = get_object_vars($d);   }  if (is_array($d)) {     return array_map(__FUNCTION__, $d);   } else {     return $d;   } }
Copy after login
Copy after login
bool(true)
我还想用phpQuery实现的 看你写出来了我就不贴出来了

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template