Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

converting jtree to xml

Posted on 2004-09-13
9
Medium Priority
?
665 Views
Last Modified: 2012-05-05
I should use the following method to create my XML

public void toXml(org.jdom.Element element)
{
}

This method is inside my JTree model class, so I have access to the root node.

The XML i generate is 1 tag inside a much bigger XML doc, it will look like this:
<filter-definition>
     <filter-group logic="AND">
            <filter field="size" operator="=" value="24" />
            <filter field="color" operator="=" value="red" />
            <filter-group logic="AND">
                      <filter field="size" operator="=" value="24" />
                      <filter field="color" operator="=" value="red" />

So each DefaultMutableTreeNode in tree can be of type:
filter-group (allows children, the root node is this type)
or
fliter (does not allow children)

what would the code look like in my recursive function?? (because children can also be of type filter-group)
and how do i wrap it all in the <filter-definition> tag.  Here is a non-recursive example i am looking at (which might help):

/**
   * add this profile to element (rootElement)
   */
  public void toXml(Element element)
  {
    Element fieldsElement = new Element("fields");
    element.addContent(fieldsElement);
    Field field;
    Iterator iterate = getList().iterator();
    // iterate through all fields in profile
    while (iterate.hasNext())
    {
      field = (Field) iterate.next();
      field.toXml(fieldsElement);
    }
  }
0
Comment
Question by:cathalmchale
  • 5
  • 2
9 Comments
 
LVL 6

Expert Comment

by:expertmb
ID: 12043088
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12043155
needs recursion to traverse each node and then create the element node.
0
 

Author Comment

by:cathalmchale
ID: 12051882
Solution (for those interested ;-)

 public void toXml(Element element)
    {
      Element filterDefinition = new Element("filter-definition");
      element.addContent(filterDefinition);
      // firstly the tree root node is a filter-group
      Element groupElement = new Element("filter-group");
      FilterJHandler.FilterJTreeGroup rootGroup = (FilterJHandler.FilterJTreeGroup) root.getUserObject();
      rootGroup.toXml(groupElement);
      filterDefinition.addContent(groupElement);

      // now call the recursive FN to recursively generate the rest!
      recursiveXml(groupElement, root);

    }

private void recursiveXml(Element element, DefaultMutableTreeNode parent)
    {
      Enumeration children = parent.children();
      while (children.hasMoreElements())
      {
        DefaultMutableTreeNode child = (DefaultMutableTreeNode) children.nextElement();
        if (child.getAllowsChildren())
        { // child is a filter-group
          Element groupElement = new Element("filter-group");
          FilterJHandler.FilterJTreeGroup filterGroup = (FilterJHandler.FilterJTreeGroup) child.getUserObject();
          filterGroup.toXml(groupElement);
          element.addContent(groupElement);
          // recurse
          recursiveXml(groupElement, child);
        }
        else
        { // child is a filter
          Element filterElement = new Element("filter");
          FilterJHandler.FilterJTreeFilter filter = (FilterJHandler.FilterJTreeFilter) child.getUserObject();
          filter.toXml(filterElement);
          element.addContent(filterElement);
        }
      }
    }
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:expertmb
ID: 12051894
>>Solution (for those interested ;-)
greate !!!
i was working on this.
points to cathalmchale :-)

mb...
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12051951
can you plz post the complete code.
0
 

Author Comment

by:cathalmchale
ID: 12052187
what else do u want - here's the other objects toXml() methos, but they're quite simple:

 public void toXml(Element element)
    {
      element.setAttribute("logic", this.toString());
    }

public void toXml(Element element)
    {
      element.setAttribute("field", varVal);
      element.setAttribute("operator", opVal);
      element.setAttribute("value", fieldVal);     }
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12061150
no problem!!!
:-)
mb...
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12065844
PAQ-ing the question and refunding 250  points

Thanks expertmb !

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

916 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