我使用 jdom2 从 xml 字符串中获取元素列表。并希望通过 getChildText 对其进行排序。
这是我的比较器:
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"
那我该怎么办?