[jdom-interest] RE: Need help

Syloke Soong ssoong at protedyne.com
Tue Mar 20 14:47:31 PST 2007


I wish to suggest that the whole chunk of code be broken into individual
methods, so that the name of each method reflects what we wish to
accomplish. To make it easier to follow.

I am suggesting the following incomplete code, where I give only the
traverseXXX methods but not the processXXX methods. By doing this, we
don't have to use XPath at all because you are merely processing
immediate children.


[code]
public final static Element traverseTreatmentplans(Element rootElem)
{
  List children = rootElem.getChildren();
  Element Treatmentplan = processTreatmentplan(rootElem);
  for (int i=0; i<children.size(); i++)
    Treatmentplan.addContent(
      traversePlansGroup((Element)children.get(i))
    );
}

public final static Element traversePlansGroup(Element
plansGroupElement)
{
  List children = plansGroupElement.getChildren();
  Element PlansGroup = processPlansGroup(plansGroupElement);
  for (int i=0; i<children.size(); i++)
    PlansGroup.addContent(
      traversePlan((Element)children.get(i))
    );
}

public final static Element traversePlan(Element planElement)
{
  processPlan(planElement);
  processGoal(planElement.getChild());
}

public final static Element processXXX(Element planElement)
{
//.... do your cloning here.
//... return clonedElement
}

[/code]


Once the code is debugged, then compact all the traverse methods into
one recursive method:


[code]
public final static Element traverse(Element e, int whomDoYouCall)
{
  List children = e.getChildren();
  Element clone = null;
  switch
  {
    case 0:
      clone = processTreatmentplans(e);
    case 1:
      clone = processPlansGroup(e);
    case 2:
      clone = processPlan(e);
    case 3:
      clone = processGoal(e.getChild());
    default:
      return null;
  }
  
  if (whomDoYouCall<3)
    for (int i=0; i<children.size(); i++)
      clone.add(
        traverse((Element)children.get(i), whomDoYouCall++)
      );
  
  return clone;
}


public static void main[]
{
// ...
  traverse ((Element)rootElem, 0);
} 

[/code]


~~~~
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the sender immediately. Please note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the company. Even though this company takes every precaution to ensure this email is virus-free, the recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email.
Protedyne Corporation, 1000 Day Hill Rd, Windsor, CT 06095, USA,                                                                  
www.protedyne.com



More information about the jdom-interest mailing list