0

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?

1
  • I removed the XSLT and SAX tags, since you explicitly asked for Java and DOM solutions. Commented Apr 9, 2010 at 8:32

2 Answers 2

0
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); 
    } 
}   
Sign up to request clarification or add additional context in comments.

Comments

0

This should start you off. Node objects are of the type Node.

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.