Creating a class using Generics

I have a task which is to take the element of an xml file and create a class by the name of the element and pass the attributes as parameters of the constructor of that class.
If the element is Abc a class by name Abc should be formed. The xml may consist of different elements at different times. I thought of approaching the issue using generics as the name of the class is not known ahead of time. I am getting it. Could anyone help me out of this.
//After parsing the xml file the following method should generate the class   
 public void generateClassNode()
    {
        String xmlFile="Member.xml";
        Node classNode=null;
        Document doc=null;
        System.out.println("Entered generate class node");
        NodeFilter filter = new NodeFilter() {
        public short acceptNode(Node n) {
        if (n.getNodeType() == Node.ATTRIBUTE_NODE || n.getNodeType()==Node.ELEMENT_NODE)
        {
          if (((Text) n).getData().trim().length() == 0)
            return NodeFilter.FILTER_REJECT;
        }
        return NodeFilter.FILTER_ACCEPT;
      }
    };
   
    int whattoshow = NodeFilter.SHOW_ELEMENT & NodeFilter.SHOW_ATTRIBUTE;
   
    if((doc=parseFile(xmlFile))!=null)
    {
        System.out.println("Before traversal ...");
        DocumentTraversal docTraverse =(DocumentTraversal)doc;
        TreeWalker twlk = docTraverse.createTreeWalker(doc.getDocumentElement(), whattoshow, filter, false);
        System.out.println("Before for loop");
        Node rootnode = twlk.getRoot();
        //NodeList nlist = rootnode.getChildNodes();
        NodeList nlist = doc.getElementsByTagName("*");
        System.out.println("The size of node list is "+ nlist.getLength());       
        //for(Node n=twlk.firstChild(); twlk.lastChild()=null; n=twlk.nextSibling())
        for(int i=0;i<nlist.getLength();i++)
        {
            System.out.println("Inside for loop");
            Node n=nlist.item(i);
           
            if(n.getNodeType()==Node.ELEMENT_NODE || n.getNodeType()==Node.ATTRIBUTE_NODE)
            {         
                String element=n.getNodeName();
                generateClass(element);
                System.out.println(element);  
            }
            if(n.getNodeType()==Node.ATTRIBUTE_NODE)
            {
                HashMap attributeBucket = new HashMap();
                String element=n.getNodeName();
                String value=n.getNodeValue();
                System.out.println(element+"= "+value); 
            }       
        }
    } 
  }
 
   //The following code generates the class of the string name
    void generateClass(String className)
    {
        String classStr=generateUcasechar(className);
        Class<T> tclass;
        tclass = (<T>)Class.forName(classStr);
       
    }
   
    //Following class changes the first char of the string into upper case to be suitable   //for the being created as class
    String generateUcasechar(String className)
    {
        StringBuilder name = new StringBuilder(className);
        name.setCharAt(0, (char)(name.charAt(0) -32));
        for(int i = 1 ; i < name.length() ; i++){
            if(name.charAt(i -1) == ' ' && name.charAt(i) != ' '){
                name.setCharAt(i, (char)(name.charAt(i) -32));
            }
        }
        return name.toString();
    }

Open in new window

rmvprasadAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
It might be possible to use an API for unmarshalling using reflection
0
 
CEHJCommented:
I wouldn't reinvent the wheel. Commons Digester will do this for you, along with a host of other things
0
 
rmvprasadAuthor Commented:
Thanks. I have looked to it. Could you please explain it with using Generics so I can get used to generics.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
rmvprasadAuthor Commented:
Another main reason for not showing intrest in common Digester is it is using xpath. So the path and the element needs to be known ahead of time. Here in my case it needs to generate a class based on the name of the element. Common Digester sample code is pasted below.

    Digester digester = new Digester();
    // This method pushes this (SampleDigester) class to the Digesters
    // object stack making its methods available to processing rules.
    digester.push(this);
    // This set of rules calls the addDataSource method and passes
    // in five parameters to the method.
    digester.addCallMethod("datasources/datasource", "addDataSource", 5);
    digester.addCallParam("datasources/datasource/name", 0);
    digester.addCallParam("datasources/datasource/driver", 1);
    digester.addCallParam("datasources/datasource/url", 2);
    digester.addCallParam("datasources/datasource/username", 3);
    digester.addCallParam("datasources/datasource/password", 4);
   
    // This method starts the parsing of the document.
    digester.parse("datasource.xml");
  }
  // Example method called by Digester.
  public void addDataSource(String name,
                            String driver,
                            String url,
                            String userName,
                            String password)
0
 
objectsCommented:
its not really appropriate for generics. with generics you can wite code to operate on different classes of objects, but you need to be able to pass the type involved.

eg. to crete a collection you need to know what type of collection you want.

List s = new ArrayList();

0
 
rmvprasadAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.