Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Exporting Query Results as XML

Hi experts,

Maybe there is a better way than what i'm doing.  

Im using mySQL and Hibernate...

I want to convert database info to XML, but i think the http://www.hibernate.org/186.html won't give me what i want.


I have a tree of data which I manually inputted in my database.
the roots have REF=0, and each subcategory has the REF of its parent.

id     category_name     REF
---------------------------
1      category1                 0
2      category2                 0
3      category1.1              1
4      category1.2              1
5      category2.1              2
...

I need to format this tree to XML.  So i'm trying to think of a good algorithm.

I figure I'll start by downloading the whole table

Then

public String getTreeInXML(ArrayList categories) {
   
   TreeMap root = new TreeMap();
   Iterator itr = categories.iterator();
   
   while (itr.hasNext()) {
      Category cat = (Category)itr.next();
      if (cat.getRef().intValue() == 0)
         root.put(cat.getRef(), );
      else
         ...        
   }

um, ok, so i will come back to this.  be back to work on it later...
}
0
dbrownell83
Asked:
dbrownell83
  • 3
1 Solution
 
hoomanvCommented:
0
 
dbrownell83Author Commented:
i need algorithmic help more than simple pojo conversion.

this is the latest code... still thinking about it.  there has GOT to be a better way


      // Daniel Brownell 2006
      public String getTreeInXML(ArrayList categories) {
               
               Hashtable allCategories = new Hashtable();
               Hashtable rootTable = new Hashtable();
               
               while (categories.size() != 0) {
                    
                     Iterator itr = categories.iterator();
                    
                     while (itr.hasNext()) {
                        Subjectcategory cat = (Subjectcategory)itr.next();
                        allCategories.put(cat.getSubjectCategoryId(), cat);
                        
                  // if they are the main categories
                        if (cat.getRefSubjectHeader().intValue() == 0){
                           Hashtable subTable = new Hashtable();
                           subTable.put(cat.getSubjectCategoryId(), cat);
                           rootTable.put(cat.getSubjectCategoryId(), subTable);
                        }
                  
                  // otherwise, they are subcategoriez
                        else {
                               Hashtable parentReference = (Hashtable)rootTable.get(cat.getRefSubjectHeader());
                             //if parentRef is null, we need to put it in the allCategory List
                               if (parentReference != null) {

                                     Hashtable childReference = new Hashtable();
                                     childReference.put(cat.getSubjectCategoryId(), cat);
                                     rootTable.put(cat.getSubjectCategoryId(),childReference);
                                     
                               }
                              
                               else {
                              
                                     parentReference = (Hashtable) allCategories.get(cat.getRefSubjectHeader());
                                     
                                     if (parentReference == null) {
                                           allCategories.put(cat.getSubjectCategoryId(), cat);
                                           //it just hasnt got there yet.
                                           continue;
                                     }
                                     else {
                                           Hashtable childReference = new Hashtable();
                                           childReference.put(cat.getSubjectCategoryId(), cat);
                                           parentReference.put(cat.getSubjectCategoryId(),childReference);
                                     }
                                            
                               }
                                
                        }
                        
                        categories.remove(cat);
                     }
               }
               
  //at this point, the bottom up way of saving the data
  // has been turned upside down, so that a rootTable
  // can call .values(), and have the children,
  // and iterate through those values as long as there are child trees to do so
               
               
               
      }
0
 
dbrownell83Author Commented:
OK,

so there was a better way
i used recursion and MYSQL select statements.

try {
                
      SubjectcategoryDAO dao = new SubjectcategoryDAO ();
                
      Subjectcategory root = (Subjectcategory)dao.findByProperty("subjectCategoryId", 78).get(0);
      System.out.println(root);
                xml = new StringBuffer();
      xml = dao.getTreeInXML(xml, root);
             System.out.println(xml);
}
    catch (Exception e) {
          e.printStackTrace();
    }




      public StringBuffer getTreeInXML(StringBuffer xml, Subjectcategory cat)
       throws Exception {
            List list = findByProperty("refSubjectHeader", cat.getSubjectCategoryId());
            //
            Iterator itr = list.iterator();
            while (itr.hasNext()) {
                  Subjectcategory child = (Subjectcategory)itr.next();
                  xml = xml.append("<").append(child.getSubjectCategoryName()).append(">");
                  xml = getTreeInXML(xml, child);
                  xml = xml.append("</").append(child.getSubjectCategoryName()).append(">");
            }
            return xml;
          
      }  
      
0
 
dbrownell83Author Commented:
ok, since i answered my own question,
can someone tell me if the first way could have worked?  
0
 
DarthModCommented:
Closed, 500 points refunded.
DarthMod
Community Support Moderator
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now