Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

converting jtree to xml

Posted on 2004-09-13
9
Medium Priority
?
664 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
[X]
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
  • 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
Independent Software Vendors: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

704 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