Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.chainon-marquant.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!74.125.46.80.MISMATCH!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Mausam Newsgroups: comp.lang.java.programmer Subject: Re: what is the bettter/performant way to compare org.w3c.dom.DocumentFragment Date: Tue, 17 Jan 2012 17:56:49 -0800 (PST) Organization: http://groups.google.com Lines: 25 Message-ID: <11466894.1272.1326851809373.JavaMail.geo-discussion-forums@prdh15> References: <16090858.1983.1326812606093.JavaMail.geo-discussion-forums@prig11> Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 122.179.102.153 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1326851809 1424 127.0.0.1 (18 Jan 2012 01:56:49 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 18 Jan 2012 01:56:49 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=122.179.102.153; posting-account=BE4JzgoAAACLX-aQYawit5MkTNnptKNs User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11445 On Tuesday, 17 January 2012 23:02:29 UTC+5:30, Jeff Higgins wrote: > On 01/17/2012 10:03 AM, Mausam wrote: > > I have a java class, whose contains a DocumentFragment. > > > > In the equals method of my class, I am converting the DocumentFragment to a String and comparing an equals on the String. > > > > I know this is not the best way, because "attributes" e.g can change order in Element of DocumentFragment, or e.g documents differ only in the sequence of unordered elements. > > > > So in such cases this equality will fail. > > > > Please suggest a better approach. > A my class is equal to another my class if and only if ... Thanks Jeff, I understand what you mean. BTW, I was checking the API http://docs.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/Node.html#isEqualNode%28org.w3c.dom.Node%29 The attributes NamedNodeMaps are equal. This is: they are both null, or they have the same length and for each node that exists in one map there is a node that exists in the other map and is equal, although not necessarily at the same index. The childNodes NodeLists are equal. This is: they are both null, or they have the same length and contain equal nodes at the same index. Note that normalization can affect equality; to avoid this, nodes should be normalized before being compared. Here for attributes, they take care of "NOT necessarily at the same index" but in case of childNodes its not being taken care of. So if there is a sequence of unordered elements ( and ) they will be treated as NOT equal. So either I iterate through each node and attribute and do a comparison. That's the fall back. But before that, I wanted to check the experts if there are better options.