tags:

views:

42

answers:

2

I am trying to re-arrange all the attributes of the XML file. Need to group all the attributes of each node.

<?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>

Here is the required output XML file:

<?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>

Can this be possible with XML DOM in JAVA?

A: 

This should start you off. Node objects are of the type http://java.sun.com/j2se/1.4.2/docs/api/org/w3c/dom/Node.html
I am not using foreach loops because Node.getChildNodes() returns getLength, and item(i) but no iterator.

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)
   }

}
mikek3332002
A: 
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); 
    } 
}   
markusk