Hi all I am using XmlBeans 2.3.0/2.4.0 with the java source 1.5 flag turned on, which provides getXList() methods for sequences. I found that if you try to sort these lists using a java.util.Collections.sort(List, Comparator) the lists get corrupted!
This is the case because changes in the XML are reflected in the array returned by getXList()toArray() and the sort function doesn't expect this. First, I use this XSD for testing <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace=""> <xsd:element name="test" type="Test"/> <xsd:complexType name="Test"> <xsd:sequence> <xsd:element name="parent" type="Parent" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="Parent"> <xsd:sequence> <xsd:element name="child" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:schema> Now if I run the following code public void testListToArray() { Test test = Test.Factory.newInstance(); test.addNewParent().set(newParent("a")); Object[] array = test.getParentList().toArray(); test.setParentArray(0, newParent("b")); System.out.println(Arrays.toString(array)); } private Parent newParent(String child) { Parent parent = Parent.Factory.newInstance(); parent.addChild(child); return parent; } The result is [<child>b</child>], where we expect [<child>a</child>] It creates a Test object that contains a single Parent object with child 'a'. By invoking the getParentList().toArray() we get an array with Parent 'a'. Setting the parent to 'b' in the XML also affects the instance of parent in the Array! The trouble is that java.util.Collections.sort(List, Comparator) does not expect this behavior and goes haywire. Example code: public void testSort() throws Exception { Test test = Test.Factory.newInstance(); test.addNewParent().set(newParent("3")); test.addNewParent().set(newParent("2")); test.addNewParent().set(newParent("1")); Collections.sort(test.getParentList(), new ParentComparator()); System.out.println(test); } private static class ParentComparator implements Comparator<Parent> { public int compare(Parent o1, Parent o2) { return o1.getChildArray(0).compareTo(o2.getChildArray(0)); } } Result: <xml-fragment> <parent> <child>1</child> </parent> <parent> <child>2</child> </parent> <parent> <child>1</child> <!-- What!? --> </parent> </xml-fragment> Please let me know whether this is a bug and what we can do about it. I would really like to be able to use 'in-place-sorting', but now it's not working! Thanks in advance. Hes Siemelink

