0

[Edit] Any algorithm/java code example to travserse and construct tree (not binary) would also be helpful

I am trying to create xml. rows and columns sizes are dynamic from database

(Ex: In this case, structure is country->state->projectNumber. It could also be country->state->department->buildingNumber->projectNumber)

You may or may not use the methods i wrote in TestMain class. I prefer not to use org.w3c.dom.Node classes due to some reasons. But not compulsory. If you help me using w3c.dom.Node, I will try to convert as per my needs.

xml should look like this.

<root>
  <value name="USA">
      <value name="NY">
        <value name="p1"></value>
        <value name="p2"></value>
        <value name="p3"></value>
        <value name="p4"></value>
      </value>
      <value name="LA">
        <value name="p6"></value>
        <value name="p7"></value>
        <value name="p8"></value>
      </value>      
      ...
      ...
  </value>
</root>



package com;


public class TestMain {

public static void main(String[] args) {
    String[][] dbResult= new String[10][3];
    dbResult[0][0]="USA";dbResult[0][1]="NY";dbResult[0][2]="P1";
    dbResult[1][0]="USA";dbResult[1][1]="NY";dbResult[1][2]="P2";
    dbResult[2][0]="USA";dbResult[2][1]="NY";dbResult[2][2]="P3";
    dbResult[3][0]="USA";dbResult[3][1]="NY";dbResult[3][2]="P4";
    dbResult[4][0]="USA";dbResult[4][1]="LA";dbResult[4][2]="P5";
    dbResult[5][0]="USA";dbResult[5][1]="LA";dbResult[5][2]="P6";
    dbResult[6][0]="USA";dbResult[6][1]="LA";dbResult[6][2]="P7";
    dbResult[7][0]="USA";dbResult[7][1]="OH";dbResult[7][2]="P8";
    dbResult[8][0]="USA";dbResult[8][1]="OH";dbResult[8][2]="P9";
    dbResult[9][0]="USA";dbResult[9][1]="TX";dbResult[9][2]="P10";

            String xmlStr="<root name=\"myName\" style=\"Horizontal\"></root>";
    TestMain.createXml(xmlStr,dbResult);
}

private static void createXml(String xmlStr,String[][] dbResult) {
    int arrayItr =0;
    System.out.println(dbResult.length);
    while(arrayItr<dbResult.length){
        String[] currRow=dbResult[arrayItr++];
        int columnNumber=1;
        for(String currValue:currRow){
            if(arrayItr==1){
                xmlStr = TestMain.insertChild(xmlStr, columnNumber, currValue);
            }else{
                /*if(TestMain.valueOfAttribute(xmlStr, "name").equalsIgnoreCase(currValue)){
                    continue;
                }else{

                }*/
                for(int childNumber=1;childNumber<TestMain.getChildrenCount(xmlStr);childNumber++){
                    TestMain.getChildrenByNum(xmlStr,childNumber);
                }


            }
                columnNumber++;
        }
        //System.out.println();
    }
    System.out.println(xmlStr);

}

private static int getChildrenCount(String xmlStr) {
    char[] chars=xmlStr.toCharArray();
    int counter=0;
    for(int currItr=0;currItr<chars.length;currItr++){
     char currChar=chars[currItr];
        if(currChar=='<' && chars[currItr+1]!='/'){
            counter++;
        }
    }
    return counter-1;
}

private static Object getChildrenByNum(String xmlStr, int childNumber) {
    // TODO Auto-generated method stub
    return null;
}

public static String insertChild(String xmlStr, int position, String valueInsert){
    System.out.println("Entered : insertChild "+xmlStr.length() );

    //xmlStr = xmlStr.substring(xmlStr.indexOf(">")+1,xmlStr.lastIndexOf("<"));
    int index=0;
    for(int counter=0;counter<position;counter++){

        index= xmlStr.indexOf(">",(index+1));
        //index=index+tempIndex+1;
        //System.out.println(xmlStr);
    }
    //System.out.println("Index: "+index);
    String op=xmlStr.substring(0,index+1) +
            "<value name=\""+valueInsert+"\"></value>" +xmlStr.substring(index+1);  

    //System.out.println("output in insChild Method: "+op);
    //xmlStr = insertChild(xmlStr,valueCompare,valueInsert);
    return op;
}
public static int findPositionToInsert(String xmlStr, String valueCompare,int pos){
    //System.out.println("Entered : findPosition");
    if(TestMain.valueOfAttribute(xmlStr, "name").equalsIgnoreCase(valueCompare)){
        return pos;
    }else if(TestMain.hasChildren(xmlStr)){
        pos++;
        xmlStr = xmlStr.substring(xmlStr.indexOf(">")+1,xmlStr.lastIndexOf("<"));
        pos=TestMain.findPositionToInsert(xmlStr, valueCompare,pos);

    }else{
        return -1;
    }
    return pos;
}
public static String valueOfAttribute(String xml,String attributeName){
    String valueOfAttribute=null;
     int index = xml.indexOf(attributeName);
     char c=xml.charAt(index+attributeName.length());
     //System.out.println("C- "+c);
     int i=index+attributeName.length();//i<xml.indexOf(">");i++){
         //if(c=='='){
             String subStr=xml.substring(i+2,xml.length());
             //System.out.println("Input Xml "+xml+" -SubStr "+subStr);
             int secondIndex=subStr.indexOf("\"");
             //System.out.println("sec index "+secondIndex);
             String value=subStr.substring(0,secondIndex);
         //}

     return value;

    //return valueOfAttribute;
}
public static boolean hasChildren(String xml){
    int index = xml.indexOf(">");
    if(xml.charAt(index-1)!='/'){
        String[] splitStr = xml.split(">");
        if(splitStr.length>1){
            return true;
        }
    }

    return false;
}

}

2 Answers 2

2

maybe you should do that with the JAXB libary:

see an example here: http://jaxb.java.net/tutorial/section_1_3-Hello-World.html#Hello%20World

regards

micha

Sign up to request clarification or add additional context in comments.

1 Comment

If you have large amount of data in db to put in xml, it may be a good idea to use Stax instead.
0

Implemented using Example 3 in http://myarch.com/treeiter/traditways

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.