[jdom-interest] XML equivalence

Shaun Smith shauns at pixology.com
Thu Aug 14 07:17:40 PDT 2003


> 
> Any comments?
> 

Inspired by the ugly serialization deep clone trick:

public class ElementEqualityTest extends TestCase
{

	private Element a;
	private Element b;
	private Element c;

	protected void setUp() throws Exception
	{
		a = new Element("foo");
		b = new Element("bar");
		c = new Element("foo");
	}

	
	public void testElementEquality() throws Exception
	{
		assertFalse(equals(a,b));
		assertTrue(equals(a,c));
	}
	
	public void testWithChildren() throws Exception
	{
		a.addContent( new Element("bar"));
		assertFalse(equals(a,c));
		c.addContent( new Element("bar"));
		assertTrue(equals(a,c));
		
		a.setAttribute("abc","xyz");
		
		assertFalse(equals(a,c));
		
		c.setAttribute("abc","xyz");
		assertTrue(equals(a,c));
	}
	
	public boolean equals(Element a, Element b) throws IOException
	{
		return elementToString(a).equals(elementToString(b));
	}

	public String elementToString(Element element) throws IOException 
	{
		StringWriter capture = new StringWriter();
		XMLOutputter serializer = new XMLOutputter();	
		serializer.output(element,capture);
		return capture.toString();
	}	
}



More information about the jdom-interest mailing list