[erlang-questions] Compare XML string

Sand T sand.softnet1@REDACTED
Tue Jun 29 06:53:27 CEST 2010


Any suggestions from the team?

Regards,
Sandeep

> Hi Michael,
>
> I need to compare two XML string and the XML string attribute order,
> value can be different as you pointed out in your explanation. The
> comparison should give *false* only if the value of attribute is
> different or some attribute is missing, It should not give *false* if
> order of attribute is different. However, when I perform,
>
>>   {Given, _} = xmerl_scan:file("Given.xml"),
>>   {Expected,_} = xmerl_scan:file("Expected.xml"),
>> --then directly compare the resulting data structures in the obvious (?)
>> way:
>>   Virtually_identical = (Given =:= Expected)
>
> it gives me *false* if the order of attribute is different. The two
> example string that I used is
>
> S="<xml session=\"123\"><response type=\"progress\" code=\"100\"
> im=\"IM\" login=\"L\">Connected To Server</response></xml>".
>
> Exp = "<xml session=\"123\"><response type=\"progress\" im=\"IM\"
> code=\"100\" login=\"L\">Connected To Server</response></xml>".
>
> Regards,
> Sandeep
>
> On Fri, Jun 25, 2010 at 8:56 PM, Michael Turner
> <michael.eugene.turner@REDACTED> wrote:
>> I feel like I must be missing something in your question.  Let's say that
>> the Given XML is in a file Given.xml, the Expected in a file Expected.xml.
>>  Just read them in--
>>   {Given, _} = xmerl_scan:file("Given.xml"),
>>   {Expected,_} = xmerl_scan:file("Expected.xml"),
>> --then directly compare the resulting data structures in the obvious (?)
>> way:
>>   Virtually_identical = (Given =:= Expected)
>> Of course, the two files could be different in certain inconsequential
>> details, such as having have the same set of attributes in an XML element
>> but in a different order, and/or having different tag formatting in
>> whitespace.  E.g., Expected.xml might literally have
>>   <sometag id="me" value="1 cent"/>
>> while Given.xml had
>>   <sometag
>>         value = "1 cent"
>>         id = me
>>   />
>> but the comparison above with Given with Expected (the resulting tree
>> structures) would still yield true all other things being equal.  Isn't that
>> what you want, though?
>> If you want to know the contents of the two files *AS STRINGS* are identical
>> (regardless of whether they are XML, I suppose), why not just read them in
>> (say as binaries) and compare them?
>> -michael turner
>>
>> On Fri, Jun 25, 2010 at 3:43 PM, Sand T <sand.softnet1@REDACTED> wrote:
>>>
>>> Hi,
>>>
>>> I need to compare two given XML string to ensure that the two strings
>>> are identical or not at runtime. Does anyone have any idea on how to
>>> do this in erlang?
>>>
>>> Following are some of the examples
>>> Given String
>>> <response type=\"progress\" im=\"IM\" login=\"ABC\"
>>> code=\"102\">Authentication Successful ...</response>
>>> Exp String:
>>> <response type=\"progress\" im=\"IM\" login=\"ABC\"
>>> code=\"102\">Authentication Successful ...</response>
>>>
>>> Eq 2:
>>> Given String
>>> <response type=\"progress\" im=\"IM\" login=\"ABC\"
>>> code=\"102\">Authentication Successful ...</response>
>>> <buddies im=\"IM\" login=\"ABC\"><buddy name=\"XYZ\" nickname=\"\"
>>> status=\"offline\" custom=\"Is offline\" group=\"Friends\"
>>> blocked=\"no\"/></buddies>
>>>
>>> Expected:
>>> <response type=\"progress\" im=\"IM\" login=\"ABC\" >Authentication
>>> Successful ...</response>
>>> <buddies im=\"IM\" login=\"ABC\" ><buddy name=\"XYZ\" nickname=\"\"
>>> status=\"online\" custom=\"Is offline\" group=\"Friends\"
>>> blocked=\"no\"/></buddies>
>>>
>>> Regards,
>>> Sandeep
>>>
>>> ________________________________________________________________
>>> erlang-questions (at) erlang.org mailing list.
>>> See http://www.erlang.org/faq.html
>>> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>>>
>>
>>
>


More information about the erlang-questions mailing list