比较SimpleXml对象 [英] Compare SimpleXml Object

查看:63
本文介绍了比较SimpleXml对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较两个SimpleXML Objects.

其中一个是从数据库中获取的,另一个是从XML API中获取的,但是无论XML是否完全相同,结果始终为false.

One is fetched from DB and the other one from a XML API, but the result is always false, whether the XML are in fact identical or not.

我做错了什么?

$objDbXml   = simplexml_load_string($objReisen->xml); // XML from DB           
$objApiXml  = simplexml_load_string(getXMlFromApi()); // XML from Api
var_dump($objDbXml->Reise->Z_LEISTUNGEN == $objApiXml->Reise->Z_LEISTUNGEN);
// Result is always false

var_dump($ objDbXml-> Reise-> Z_LEISTUNGEN,$ objApiXml-> Reise-> Z_LEISTUNGEN)的输出:

The output of var_dump($objDbXml->Reise->Z_LEISTUNGEN , $objApiXml->Reise->Z_LEISTUNGEN):

object(SimpleXMLElement) #69 (1) {
    ["TextLine"]= > array(11) {
        [0] = > string(43) "Erlebnisreise mit höchstens 13 Teilnehmern" 
        [1] = > string(39) "Durchführungsgarantie ab 4 Teilnehmern" 
        [2] = > string(127) "Linienflug mit South African Airways von Frankfurt a.M. nach Kapstadt und zurück von Port Elizabeth (von München auf Anfrage)" 
        [3] = > string(28) "Reiseminibus mit Klimaanlage" 
        [4] = > string(111) "Übernachtungen in Hotels und Lodges sowie 2 Übernachtungen in einer exklusiven Lodge im Kariega Game Reserve" 
        [5] = > string(67) "Täglich Frühstück, 2 x Mittagessen, 4 x Abendessen, 1 Weinprobe" 
        [6] = > string(123) "1 Safari im Addo-Elephant-NP; 2 Safaris im offenen Geländewagen, 1 Wandersafari und 1 Bootsfahrt im Kariega Game Reserve" 
        [7] = > string(41) "Nationalparkgebühren und Eintrittsgelder"
        [8] = > string(14) "Reiseliteratur" 
        [9] = > string(43) "Zertifikat über 100 m² Regenwald für Sie" 
        [10] = > string(42) "Deutsch sprechende Chamäleon-Reiseleitung"
    }
}

object(SimpleXMLElement) #67 (1) {
    ["TextLine"]= > array(11)
    {
        [0] = > string(43) "Erlebnisreise mit höchstens 12 Teilnehmern" 
        [1] = > string(39) "Durchführungsgarantie ab 4 Teilnehmern" 
        [2] = > string(127) "Linienflug mit South African Airways von Frankfurt a.M. nach Kapstadt und zurück von Port Elizabeth (von München auf Anfrage)" 
        [3] = > string(28) "Reiseminibus mit Klimaanlage" 
        [4] = > string(111) "Übernachtungen in Hotels und Lodges sowie 2 Übernachtungen in einer exklusiven Lodge im Kariega Game Reserve" 
        [5] = > string(67) "Täglich Frühstück, 2 x Mittagessen, 4 x Abendessen, 1 Weinprobe" 
        [6] = > string(123) "1 Safari im Addo-Elephant-NP; 2 Safaris im offenen Geländewagen, 1 Wandersafari und 1 Bootsfahrt im Kariega Game Reserve" 
        [7] = > string(41) "Nationalparkgebühren und Eintrittsgelder" 
        [8] = > string(14) "Reiseliteratur" 
        [9] = > string(43) "Zertifikat über 100 m² Regenwald für Sie" 
        [10] = > string(42) "Deutsch sprechende Chamäleon-Reiseleitung"
    }
}

推荐答案

与SimpleXML一样,这里的问题是,SimpleXMLElement不是普通" PHP对象. SimpleXML不是解析器,它使用属性和方法吐出格式完整的PHP对象,而是链接到XML文档内部表示形式的实时" API.

The problem here, as so often with SimpleXML, is in the fact that a SimpleXMLElement is not a "normal" PHP object. SimpleXML is not a parser which spits out fully-formed PHP objects with properties and methods, but a "live" API linked to an internal representation of an XML document.

比较对象的手册页指出如果两个对象实例相等,它们具有相同的属性和值,并且是同一类的实例."当您在SimpleXMLElement上运行print_r()var_dump()时,它会出现,该属性具有表示子节点和属性的属性,对于使用相同XML构建的两个对象来说,这些属性是相同的.但是, actual 实现仅包含指向解析XML时创建的内存结构的指针,即使您两次解析相同的字符串,该指针也将有所不同.因此,仅将两个SimpleXMLElement对象与==进行比较就永远不会返回true.

The manual page on Comparing Objects states that "Two object instances are equal if they have the same attributes and values, and are instances of the same class." When you run print_r() or var_dump() over a SimpleXMLElement it appears to have properties representing the child nodes and attributes, which would be the same for two objects built from identical XML. However, the actual implementation contains only a pointer into a memory structure created when the XML was parsed, which will be different even if you parse the same string twice. Thus simply comparing two SimpleXMLElement objects with == will never return true.

实际的解决方案取决于您要精确比较的内容:

The actual solution depends on what exactly you want to compare:

  • 如果要查看两个文档之间XML的特定片段是否100%相同,则可以使用->asXML()来获取该文档那部分的XML字符串.例如$objDbXml->Reise->Z_LEISTUNGEN->asXML() == $objApiXml->Reise->Z_LEISTUNGEN->asXML()
  • 如果要比较一些特定的属性,最好选择这些属性并分别进行比较,这样即使它们以稍微不同的顺序出现或使用特殊字符编码,测试也会返回true稍有不同
  • if you want to see if a particular fragment of the XML is 100% identical between the two documents, you could use ->asXML() to get an XML string for that part of the document; e.g. $objDbXml->Reise->Z_LEISTUNGEN->asXML() == $objApiXml->Reise->Z_LEISTUNGEN->asXML()
  • if there are a few specific properties which you want to compare, you may be better off selecting those out and comparing them individually, so that the test returns true even if they appear in a slightly different order, or with special characters encoded slightly differently

这篇关于比较SimpleXml对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆