Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
可以使用JAVA对XML中节点的所有属性进行分组吗?_Java_Xml_Dom - Fatal编程技术网

可以使用JAVA对XML中节点的所有属性进行分组吗?

可以使用JAVA对XML中节点的所有属性进行分组吗?,java,xml,dom,Java,Xml,Dom,我正在尝试重新排列XML文件的所有属性。 需要对每个节点的所有属性进行分组 <?xml version="1.0" encoding="UTF-8"?> <subject> <param name="A" value="a" /> <study> <param name="AA" value="aa" /> <series> <param name

我正在尝试重新排列XML文件的所有属性。 需要对每个节点的所有属性进行分组

<?xml version="1.0" encoding="UTF-8"?> <subject> <param name="A" value="a" /> <study> <param name="AA" value="aa" /> <series> <param name="AAA" value="aaa" /> <dataset> <param name="AAAA" value="aaaa" /> <data> <param name="AAAAA" value="aaaaa" /> </data> </dataset> <param name="BBB" value="bbb" /> </series> </study> <param name="B" value="b" /> </subject> 以下是所需的输出XML文件:

<?xml version="1.0" encoding="UTF-8"?> <subject> <param name="A" value="a" /> <param name="B" value="b" /> <study> <param name="AA" value="aa" /> <series> <param name="AAA" value="aaa" /> <param name="BBB" value="bbb" /> <dataset> <param name="AAAA" value="aaaa" /> <data> <param name="AAAAA" value="aaaaa" /> </data> </dataset> </series> </study> </subject>
使用JAVA中的XML DOM可以做到这一点吗?

这应该是您的出发点。节点对象的类型为

我不使用foreach循环,因为Node.getChildNodes()返回getLength和item(I),但不返回迭代器

public void sortXML(Node newRoot, Node oldRoot)
{
   for(int i=0; i<oldRoot.getChildNodes().getChildNodes().length(); i++)
   {
        if(oldRoot.getChildNodes().item(i).getType==ELEMENT_NODE)
        {
            if(oldRoot.getChildNodes().item(i).getName().equals("param"))
            {
                 newRoot.appendChild(oldRoot.getChildNodes().item(i));//add param tags
            }
        }
   }
   for(int i=0; i<oldRoot.getChildNodes().length(); i++)
   {
        if(oldRoot.getChildNodes().item(i).getType==ELEMENT_NODE)
        {
            if(!oldRoot.getChildNodes().item(i).getName().equals("param"))
            {
                 newRoot.appendChild(oldRoot.getChildNodes().item(i));//add non param tags
            }
        }
   }
//reurse through the rest of the document
   for(i=0;i<oldRoot.getChildNodes().length();i++)
   {
        sortXML(newRoot.getChildNodes.item(i),oldRoot.getChildNodes.item(i)
   }

}
public void sortXML(节点newRoot,节点oldRoot)
{
对于(int i=0;i
public void groupParams(Node Node){
NodeList childNodes=node.getChildNodes();
列表参数=新的ArrayList();
对于(int i=0;i
我删除了XSLT和SAX标记,因为您明确要求使用Java和DOM解决方案。
public void groupParams(Node node) { 
    NodeList childNodes = node.getChildNodes(); 
    List<Element> paramElements = new ArrayList<Element>(); 
    for (int i = 0; i < childNodes.getLength(); i++) { 
        Node child = childNodes.item(i); 
        if (child.getNodeType() == Node.ELEMENT_NODE) { 
            if (child.getNodeName().equals("param")) { 
                paramElements.add((Element) child); 
            } else { 
                // Recursively group param elements in child node. 
                groupParams(child); 
            } 
        }
    } 
    // Remove all param elements found from source node. 
    for (Element paramElement: paramElements) { 
        node.removeChild(paramElement); 
    }
    // Sort param elements by attribute "name". 
    Collections.sort(paramElements, new Comparator<Element>() { 
            public int compare(Element e1, Element e2) { 
                String name1 = e1.getAttribute("name"); 
                String name2 = e2.getAttribute("name"); 
                return name1.compareTo(name2); 
            } 
    }); 
    // Add param elements back to source node in sorted order. 
    Node firstNonParamNode = node.getFirstChild(); 
    for (Node paramElement: paramElements) { 
        node.insertBefore(paramElement, firstNonParamNode); 
    } 
}