1
我使用jdom2從xml字符串中獲取元素列表。 並想getChildText 這個排序是我比較使用Comparator進行排序時,內容已經有父親
public class ComparatorFunc implements Comparator {
public int compare(Object arg0, Object arg1) {
Element e1 = (Element)arg0;
Element e2 = (Element)arg1;
int flag = e1.getChildText("EntType").compareTo(e2.getChildText("EntType"));
return flag;
}
}
-----這是我的代碼:
String xmlStr = request.getParameter("xmlStr");
xmlStr = xmlStr.trim();
//System.out.println(xmlStr);
ByteArrayInputStream xmlStream = null;
try {
xmlStream = new ByteArrayInputStream(xmlStr.getBytes("UTF-8"));
if (xmlStream != null) {
SAXBuilder builder = new SAXBuilder(false);
org.jdom2.Document document = builder.build(xmlStream);
Element root = document.getRootElement();
List <Element> listItem = root.getChildren("item");
ComparatorFunc comparator = new ComparatorFunc();
Collections.sort(listItem, comparator);
.....
,但我得到這個錯誤:
org.jdom2.IllegalAddException: The Content already has an existing parent "design"
那我該怎麼辦?
你從哪裏得到這個異常?在排序幫助之前創建新列表嗎? – qqilihq
@qqilihq在這一行'Collections.sort(listItem,comparator);' – chanjianyi
然後嘗試創建列表的副本。 – qqilihq