Solved

converting jtree to xml

Posted on 2004-09-13
9
658 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

808 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