Skip to main content
January 12, 2010
Question

XML Object Comparison

  • January 12, 2010
  • 2 replies
  • 1779 views

When comparing two XML objects, it appears that flexunit is reporting their equality when the values of XML elements are not equal.

Here's a valid example:

var xmlA:XML =

<root>

     <element1>text1</element1>

     <element2>text2</element2>

</root>;

var xmlB:XML =

<root>

     <element2>text2</element2>

     <element1>text1</element1>

</root>;

Assert.assertTrue(ObjectUtil.compare(xmlA, xmlB) == 0)   => true

This is correct: element order does not matter and $xmlA/root/element1 == $xmlB/root/element1

Here's an invalid example:

var xmlA:XML =

<root>

     <element1>text1</element1>

     <element2>text2</element2>

</root>;

var xmlB:XML =

<root>

     <element1>text3</element1>

     <element2>text2</element2>

</root>;

Assert.assertTrue(ObjectUtil.compare(xmlA, xmlB) == 0)   => true

This is NOT correct: $xmlA/root/element1 != $xmlB/root/element1

Can somebody confirm this?  If confirmed, is this by design or am I missing something?

Thanks!

Sam

This topic has been closed for replies.

2 replies

Participating Frequently
January 12, 2010

Did you try just checking for equality of the nodes?

xml1 == xml2

However, I suspect they are going to take element order into account as they are likely just converting it to a string and comapring

Mike

January 12, 2010

I've tried so many things, I couldn't remember.  So I tested it and it failed.

            var xmlA:XML =
                <root>
                     <element1>text1</element1>
                     <element2>text2</element2>
                </root>;
           
            var xmlB:XML =
                <root>
                     <element2>text2</element2>
                     <element1>text1</element1>
                </root>;
           
            Assert.assertTrue(xmlA == xmlB)  => fails

Thanks for the suggestion.

Sam

Participating Frequently
January 20, 2010

As you've discovered, the problem is with your comparison. XML in Actionscript are really objects under the hood.

Lets say you had this:

var xml1:Object = {element1: "text1", element2: "text2"};

var xml2:Object = {element2: "text2", element1: "text1"};

Here, just like you want to do the XML comparison, the order of the properties doesn't matter, but you want to check the values of a particular property.

So xml1 == xml2 fails not because the element values are out of order, but because the comparison occurs on the objects themselves, and not the properties in the objects.

In order for my object comparison to succeed, I would probably need to create a hash of properties from both objects and verify that the keys are the same and the values of matching keys are the same if I don't want to use any of the built in utilities for comparing objects.

So, in your example, the comparison you want to use is E4X, doing something like this:

Assert.assertTrue( xmlA.element1.text()[0], xmlB.element1.text()[0]);

I'm not sure how to write an E4X expression traversing two nodes at once, but maybe that will give you some ideas to go on?

January 12, 2010

When comparing two XML objects, it appears that flexunit is reporting their equality when the values of XML elements are not equal.

Sorry, I should have said that ObjectUtil.compare() is reporting  to be equal, not flexunit.

Is there a better way to do XML comparison in flexunit than using ObjectUtil.compare()?

Assert.assertTrue(xmlA, xmlB) seemed to fail if the elements were not in the same order.

Sam