Skip to Content

.

To write the sorting code, we can write a separate method along with execute() as follows:

 

 

public void execute(InputStream
in, OutputStream
out) {

 

      try
{

            DocumentBuilderFactory factory =

                  DocumentBuilderFactory.newInstance();

DocumentBuilder builder =
factory.newDocumentBuilder();

            Document sourceDoc = builder.parse(in);

 

            //Read

            Node mainMessage = sourceDoc.getFirstChild();

NodeList level1List =
mainMessage.getChildNodes();

 

            for (int level1Count
= 0;

                  level1Count < level1List.getLength();

                  level1Count ++) {

 

                  Node level1Node = level1List.item(level1Count);

if (“Level1”.equals(level1Node.getNodeName()))
{

                        level1Node
= this.sort (level1Node, sourceDoc);

 

 

 

…………………………………………..

…………………………………………..

Normal Java mapping code continues here

…………………………………………..

…………………………………………..

 

}

}

            } catch (Throwable t) {

                  t.printStackTrace();

            }

}

 

      private
Node sort(Node
sortNode, Document
sourceDoc) {

 

            NodeList level2List = sortNode.getChildNodes();

            String sortDataUC = null;

            String sortCurrents = null;

 

            //Loop for getting LEVEL2 structure

            for (int level2Count
= 0;

                  level2Count < level2List.getLength();

                  level2Count++) {

                  Node level2Node = level2List.item(level2Count);

 

                  if (“Level2”.equals(level2Node.getNodeName()))
{

 

                        //Loop for getting LEVEL2 Structure counter runs from
last loop counter till end

                        for (int sortLevel2Count
= level2Count
+ 1;

                              sortLevel2Count <<br />level2List.getLength();

                              sortLevel2Count++) {

 

                              Node sortLevel2Node
= level2List.item(sortLevel2Count);

                              if (“Level2”.equals(sortLevel2Node.getNodeName()))
{

 

                                    NodeList level2Elements
= level2Node.getChildNodes();

 

                                    //Loop in current LEVEL2 element to get the value of
“DATA_SORT” data which we need for further comparison

                                    for (int elementCounter
= 0;

                                          elementCounter <<br />level2Elements.getLength();

                                          elementCounter++) {

                                          Node level2Element =

                                                level2Elements.item(elementCounter);

 

                                          //Find the element with tag “DATA_SORT”. This
element will be under loop                                       

                                          if (level2Element

                                                .getNodeName()

                                                .equals(“DATA_SORT”)) {

                                                sortDataUC =

                                                      level2Element

                                                            .getFirstChild()

                                                            .getNodeValue();

                                                break;

                                          }

                                    }

 

                                    //Loop to get the element which may be shorter than above
element and need for comparison further

                                    NodeList level2SortElements
=

                                          sortLevel2Node.getChildNodes();

                                    for (int elementCounter
= 0;

                                          elementCounter <<br />level2SortElements.getLength();

                                          elementCounter++) {

                                          Node level2SortElement
=

                                                level2SortElements.item(elementCounter);

                                          if (level2SortElement

                                                .getNodeName()

                                                .equals(“DATA_SORT”)) {

                                                sortCurrents =

                                                      level2SortElement

                                                            .getFirstChild()

                                                            .getNodeValue();

                                                break;

                                          }

                                    }

 

                                    //Compare the data and switch the nodes

                                    if (sortDataUC.compareTo(sortCurrents)

0) {</p>

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply