Java 反向XML子标记

Java 反向XML子标记,java,xml-parsing,Java,Xml Parsing,在Java中反转XML中的行/标记的最佳选项是什么 e、 g 这将反转元素的所有子节点: NodeList nl = element.getChildNodes(); LinkedList<Node> nodes = new LinkedList<Node>(); for( int i = 0; i < nl.getLength(); i++ ) { nodes.addFirst( nl.item( i ) ); } for( Node node : nodes

在Java中反转XML中的行/标记的最佳选项是什么

e、 g


这将反转
元素
的所有子节点:

NodeList nl = element.getChildNodes();
LinkedList<Node> nodes = new LinkedList<Node>();
for( int i = 0; i < nl.getLength(); i++ ) {
  nodes.addFirst( nl.item( i ) );
}
for( Node node : nodes ) {
  element.appendChild( element.removeChild( node ) );
}
NodeList nl=element.getChildNodes();
LinkedList节点=新建LinkedList();
对于(int i=0;i

但是,如果您希望将特定顺序实现为
比较器
,则将节点放入使用比较器的
树集中,而不是
链接列表

这将反转
元素
的所有子节点:

NodeList nl = element.getChildNodes();
LinkedList<Node> nodes = new LinkedList<Node>();
for( int i = 0; i < nl.getLength(); i++ ) {
  nodes.addFirst( nl.item( i ) );
}
for( Node node : nodes ) {
  element.appendChild( element.removeChild( node ) );
}
NodeList nl=element.getChildNodes();
LinkedList节点=新建LinkedList();
对于(int i=0;i

但是,如果您希望将特定顺序实现为
比较器
,则将节点放入使用比较器的
树集中,而不是
链接列表
。它将所有子节点放在一个列表中,然后使用比较器对它们进行排序。我不认为您可以对属性进行排序,因为文档声明NamedNodeMaps不按任何顺序维护

public static void sortChildren(Node parent, Comparator<Node> comparator){
    NodeList children = parent.getChildNodes();
    if(children.getLength() == 0){
        return;
    }
    List<Node> nodes = new ArrayList<Node>();
    for(int i = 0 ; i < children.getLength() ; i++){
        Node n = children.item(i);
        sortChildren(n, comparator);
        nodes.add(n);
    }
    Collections.sort(nodes, Collections.reverseOrder(comparator));
    for(Node n : nodes){
        parent.appendChild(n);
    }
}

public static void main(String[] args) throws Exception {

    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("file.xml"));
    Element root = doc.getDocumentElement();

    //sort it recursively
    sortChildren(root, new DefaultNodeNameComparator());

    //print it out (for debugging)
    OutputFormat format = new OutputFormat(doc);
    format.setLineWidth(65);
    format.setIndenting(true);
    format.setIndent(2);
    Writer out = new StringWriter();
    XMLSerializer serializer = new XMLSerializer(out, format);
    serializer.serialize(doc);
    System.out.println(out.toString());
}

class DefaultNodeNameComparator implements Comparator<Node> {
    public int compare(Node arg0, Node arg1) {
        return arg0.getNodeName().compareTo(arg1.getNodeName());
    }
}
publicstaticvoidsortchildren(节点父节点、比较器、比较器){
nodelistchildrends=parent.getChildNodes();
if(children.getLength()==0){
返回;
}
列表节点=新的ArrayList();
for(int i=0;i
这是一个可以使用的递归方法。它将所有子节点放在一个列表中,然后使用比较器对它们进行排序。我不认为您可以对属性进行排序,因为文档声明NamedNodeMaps不按任何顺序维护

public static void sortChildren(Node parent, Comparator<Node> comparator){
    NodeList children = parent.getChildNodes();
    if(children.getLength() == 0){
        return;
    }
    List<Node> nodes = new ArrayList<Node>();
    for(int i = 0 ; i < children.getLength() ; i++){
        Node n = children.item(i);
        sortChildren(n, comparator);
        nodes.add(n);
    }
    Collections.sort(nodes, Collections.reverseOrder(comparator));
    for(Node n : nodes){
        parent.appendChild(n);
    }
}

public static void main(String[] args) throws Exception {

    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("file.xml"));
    Element root = doc.getDocumentElement();

    //sort it recursively
    sortChildren(root, new DefaultNodeNameComparator());

    //print it out (for debugging)
    OutputFormat format = new OutputFormat(doc);
    format.setLineWidth(65);
    format.setIndenting(true);
    format.setIndent(2);
    Writer out = new StringWriter();
    XMLSerializer serializer = new XMLSerializer(out, format);
    serializer.serialize(doc);
    System.out.println(out.toString());
}

class DefaultNodeNameComparator implements Comparator<Node> {
    public int compare(Node arg0, Node arg1) {
        return arg0.getNodeName().compareTo(arg1.getNodeName());
    }
}
publicstaticvoidsortchildren(节点父节点、比较器、比较器){
nodelistchildrends=parent.getChildNodes();
if(children.getLength()==0){
返回;
}
列表节点=新的ArrayList();
for(int i=0;i
我遇到了一个问题,在之前的评论中,我深受启发,将RSS元素反转,下面是代码:

    // RSS sorting of <items> by DESC
    NodeList nl = document.getElementsByTagName( "item" );
    LinkedList<Node> nodes = new LinkedList<Node>();
    for( int i = 0; i < nl.getLength(); i++ ) {
        nodes.addFirst( nl.item( i ) );
    }
    for( Node node : nodes ) {
        node.getParentNode().appendChild( node.getParentNode().removeChild( node ) );
    }
//按DESC对的RSS排序
NodeList nl=document.getElementsByTagName(“项”);
LinkedList节点=新建LinkedList();
对于(int i=0;i
我遇到了一个问题,在之前的评论中,我深受启发,将RSS元素反转,下面是代码:

    // RSS sorting of <items> by DESC
    NodeList nl = document.getElementsByTagName( "item" );
    LinkedList<Node> nodes = new LinkedList<Node>();
    for( int i = 0; i < nl.getLength(); i++ ) {
        nodes.addFirst( nl.item( i ) );
    }
    for( Node node : nodes ) {
        node.getParentNode().appendChild( node.getParentNode().removeChild( node ) );
    }
//按DESC对的RSS排序
NodeList nl=document.getElementsByTagName(“项”);
LinkedList节点=新建LinkedList();
对于(int i=0;i
我认为您的代码将抛出一个错误sortChildren(…),因为它接受节点和比较器,但输入是元素,Comparator@Panther24
元素扩展了节点
,因此代码可以工作。我已经运行了它。嗯,我得到了这样一个错误:“没有XMLSort类型的封闭实例是可访问的。必须使用XMLSort类型的封闭实例(例如,x.new A(),其中x是XMLSort的实例)来限定分配。”在线sortChildren(root,new DefaultNodeNameComparator());,你有没有把DefaultNodeNameComparator作为一个内部类?如果是这样的话,把它取出来,让它成为一个普通类。我认为这段代码会抛出一个错误sortChildren(…),因为它接受节点和比较器,但输入是元素,Comparator@Panther24
元素扩展了节点
,因此代码可以工作。我有