Solved

converting jtree to xml

Posted on 2004-09-13
9
659 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
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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 96
by zero exception 10 69
java example issue 5 42
Bot application - advice 3 59
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 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