Solved

converting jtree to xml

Posted on 2004-09-13
9
655 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
 
LVL 6

Expert Comment

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

mb...
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Receive file in Servlet 1 36
Passing list of object to Oracle Database Procedure 3 42
strDist challenge 35 84
@SBGen Method 3 25
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now