[jdom-interest] RE: Need help

Normadiah Mahiddin normadiah at gmail.com
Tue Mar 20 00:13:18 PST 2007


Hi Syloke Soong,
Thank you very much for your reply.
I have tried the three example code that you gave
previously. However, it still could'nt work.
The new one that you gave, I haven't try yet since
I'm outside of my place now....
For your information, currently, the result that I got
was like below:

*<treatmentplans>
  <Plans_Group ID="7">
    <Plan ID="7.1" Name="Doctor Follow Up" />
    <Plan ID="7.2" Name="Haemodialysis">
      <Goal>Detect the stage of Kidney Disease</Goal>
    </Plan>
    <Plan ID="7.3" Name="Diet in Kidney Disease">
      <Goal>Detect the stage of Kidney Disease</Goal>
      <Goal>To give patient a life</Goal>
    </Plan>
  </Plans_Group>
</treatmentplans>*

In fact, the real result that I should get should be like below:

 *<treatmentplans>
  <Plans_Group ID="7">
    <Plan ID="7.1" Name="Doctor Follow Up" />*
*            <Goal>Detect the stage of Kidney Disease</Goal>*
*    </Plan>
    <Plan ID="7.2" Name="Haemodialysis">*
*            <Goal>To give patient a life</Goal>
     </Plan>
    <Plan ID="7.3" Name="Diet in Kidney Disease">
           <Goal>To ensure the patient healthy life</Goal>
     </Plan>
  </Plans_Group>
</treatmentplans>*

The red text showed the goal that didn't include.

Below was my code:
*public static void main(String args[])
 {
  String ID="",Name="",goal="";
  Element Goal = new Element("Goal");
  Element Plan = new Element("Plan");

  //Read XML file and create XML document object
  SAXBuilder saxobj = new SAXBuilder();

  try {
   //get Document sourceDoc XML file
   Document sourceDoc = saxobj.build("PlanRepositoryV6.xml");

   //Set up the XML document builder and parse the input XML file
   Document document = null;
   document = saxobj.build( new File( "TreatmentPlans.xml" ) );
   // get the parent element in original xml file, that you want to
   // add the new data element as child to
   Element parentElemToAddChildrenTo = document.getRootElement();

   //Get XML node that we want
   XPath Plans_GroupPath = XPath.newInstance("//Plans_Group[@ID='6']" );

   List Plans_GroupList = Plans_GroupPath.selectNodes(sourceDoc);
    if ( Plans_GroupList == null ) {
     System.out.println( "Sorry, no matching element." );
    }
    else {
     for ( int k = 0; k < Plans_GroupList.size(); k++ )
     {
      /****
       * Retrieve the elements and its children
       *********/
        Element Plans_GroupElem = (Element)Plans_GroupList.get(k);
       System.out.println(Plans_GroupElem +
Plans_GroupElem.getAttributeValue("ID"));
       String PGIDString = Plans_GroupElem.getAttributeValue("ID");

       //Change PG ID from string to integer to add the PG ID value
       int PGID = Integer.parseInt(PGIDString);

       //Construct new data
       int NewPGID = PGID + 1;
       String NewPGIDString = Integer.toString(NewPGID);
      Element PlansGroup = new Element("Plans_Group");
      PlansGroup.setAttribute("ID",NewPGIDString);

       //Retrieve elements one level deep
       Iterator itr = (Plans_GroupElem.getChildren()).iterator();
      while(itr.hasNext())
      {
         Element oneLevelDeep = (Element)itr.next();
         //Element Plan = new Element("Plan");
         if(oneLevelDeep.getName().equals("Plan"))
         {
           ID = oneLevelDeep.getAttributeValue("ID");
           float PID = Float.parseFloat(ID);
           float NewPID = PID + 1;
           String NewPIDString = Float.toString(NewPID);
           Name = oneLevelDeep.getAttributeValue("Name");
           Plan.setAttribute("ID",NewPIDString);
           Plan.setAttribute("Name",Name);

           PlansGroup.addContent((Element)Plan.clone()); //This work right
         }

          //Retrieve elements two level deep
          Iterator itr1 = (oneLevelDeep.getChildren()).iterator();
       while(itr1.hasNext())
       {
          Element twoLevelsDeepElem = (Element)itr1.next();
          //Element Goal = new Element("Goal");
          if(twoLevelsDeepElem.getName().equals("Goal"))
             {
            goal = twoLevelsDeepElem.getText();
            System.out.println(goal);
            Goal.setText(goal);
           ** Plan.addContent((Element)Goal.clone());  //This work wrong

             }

          //Retrieve elements three level deep
        Iterator itr2 = (twoLevelsDeepElem.getChildren()).iterator();
        while(itr2.hasNext())
        {
           Element threeLevelsDeepElem = (Element)itr2.next();
           System.out.println(threeLevelsDeepElem +
threeLevelsDeepElem.getText());
           List fourLevelsDeep = threeLevelsDeepElem.getChildren();

           //Retrieve elements four level deep
        }
        }
       }


        /****
         * Open new XML file and write the elements and its children on it
         *********/

      parentElemToAddChildrenTo.addContent(PlansGroup);
      //output the XML document to output file -- which can be the
      //original file you read from.
      XMLOutputter outp = new XMLOutputter( Format.getPrettyFormat() );
      try {
       outp.output( document, new OutputStreamWriter(
         new FileOutputStream( "TreatmentPlans.xml" ), "UTF-8" ) );
      } catch (UnsupportedEncodingException e) {
       e.printStackTrace();
      } catch (FileNotFoundException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      }
     }//end for loop
    }//end else
  } catch(JDOMException je) {
   je.printStackTrace();
  } catch (IOException ie) {
   ie.printStackTrace();
  }
 }//end main*

To others, have any ideas?
Please help me... I really need help ......

On 3/20/07, Syloke Soong <ssoong at protedyne.com> wrote:
>
> BTW, despite last couple of weeks' debate about issuing a
> generics-friendly JDOM version, I am thinking we could also do it this
> way in Java1.5:
>
> public List <Element> cloneChildren(Element element)
> {
> List <Element>clonedElementList = new Vector();
> List <Element>origElementList = element.getChildren();
> for (Element ej: origElementList)
>    clonedElementList.add((Element)ej.clone());
>
> return clonedElementList;
> }
>
> I have not tried but I'm going to. Can anyone tell me if the above
> should work? It compiled successfully, though. If I could do it this
> way, what is the fuss on issuing a generics-friendly JDOM version all
> about?
>
>
> 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
>
> _______________________________________________
> To control your jdom-interest membership:
> http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20070320/a71eb5bb/attachment.htm


More information about the jdom-interest mailing list