[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Exporting Query Results as XML

Posted on 2006-10-19
Medium Priority
Last Modified: 2010-03-31
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


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(), );

um, ok, so i will come back to this.  be back to work on it later...
Question by:dbrownell83
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 14

Expert Comment

ID: 17767960

Author Comment

ID: 17770967
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);
                               else {
                                     parentReference = (Hashtable) allCategories.get(cat.getRefSubjectHeader());
                                     if (parentReference == null) {
                                           allCategories.put(cat.getSubjectCategoryId(), cat);
                                           //it just hasnt got there yet.
                                     else {
                                           Hashtable childReference = new Hashtable();
                                           childReference.put(cat.getSubjectCategoryId(), 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

Author Comment

ID: 17771280

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);
                xml = new StringBuffer();
      xml = dao.getTreeInXML(xml, root);
    catch (Exception e) {

      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;

Author Comment

ID: 17771285
ok, since i answered my own question,
can someone tell me if the first way could have worked?  

Accepted Solution

DarthMod earned 0 total points
ID: 17813882
Closed, 500 points refunded.
Community Support Moderator

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question