My appologies for not being clarify my intention earlier. I am
trying to identify a element in the DOM using XPath and replace
it with new element.

I have realized that how dumb I am that I could get help a lot
sooner and make everyone's life easier as well.

Your "detach" suggestion will work for what I need. Then how
can I add a new element at the same location where we detach
the old element, i.e., kinda replacing an old element with a
new element. Thank you.

Regards,


Pae



> Are you just trying to filter the hierachy or find the elements?
>
> Perhaps if you tell us more about what you need instead of trying to
> solve a piece of the puzzle we could help more.  The first option I
> showed you is probably the one you should use and just create a new root
>   element called Patient and add the nodes that come back from the
> select to it to fulfill your filtering.
>
> Sorry I could not be of more help, but, XPath returns matching nodes.
> Unfortunately Patient is now seen as a peer to Age and Gender.  Using
> "/*/*[local-name()!='ContactInfo']" will return the nodes under Patient.
>
> Another option is to simply delete ContactInfo once it is found. Do a
> selectNodes on "/*/*[local-name()='ContactInfo']" and then remove the
> items by "detach()" so they no longer show up in the parent.  Then the
> root element would no longer have ContactInfo as a child.
>
> Try this out
> //...
>    public static void main(String[] args) {
>      try {
>        System.out.println("starting main...");
>
>        SAXReader reader = new SAXReader();
>        Document document = reader.read(
> new File("patient_records.xml" ));
>        Element root = document.getRootElement();
>
>        System.out.println("xml=\n"+root.asXML());
>        List contactInfoList = root.selectNodes("//ContactInfo");
>        Object array[] = contactInfoList.toArray();
>        Element item=null;
>        for (int i =0; i < array.length; i++ ){
>          item=(Element) array[i];
>          item.detach();
>        }
>        System.out.println("xml=\n"+root.asXML());
>      }
>      catch (Exception ex) {
>        ex.printStackTrace();
>      }
>      finally {
>        System.out.println("leaving main...");
>      }
>    }
> //...
>
> Not sure why, but the whitespace from the asXML on the root the second
> time seems to have some extra <CR> embedded.  Anyone know why?
>
> Best Wishes,
> Dave
>
>
>
> Pae Choi wrote:
> > Dave,
> >
> > With the XPE tool(under the Matching Nodes tab), it returns as follows:
> >
> > <Patient>
> > <Age>
> > <Gender>
> >
> >
> > Both with Xalan and Jaxen with DOM4J, it returns as follows:
> >
> > <Patient>
> >     <Age>39</Age>
> >     <Gender>Female</Gender>
> >     <ContactInfo>
> >         <PhoneNumber>123-456-7890</PhoneNumber>
> >         <EmailAddress>[EMAIL PROTECTED]</EmailAddress>
> >     </ContactInfo>
> > </Patient>
> > <Age>39</Age>
> > <Gender>Female</Gender>
> >
> > More comments? Thank you.
> >
> > Regards,
> >
> >
> > Pae
> >
> >
> >
> > ----- Original Message -----
> > From: "David D. Lucas" <[EMAIL PROTECTED]>
> > To: "Pae Choi" <[EMAIL PROTECTED]>
> > Cc: <[EMAIL PROTECTED]>
> > Sent: Monday, November 18, 2002 5:36 AM
> > Subject: Re: [dom4j-user] XPath result from a DOM and the Rest of DOM
> >
> >
> >
> >>So, what you really want is:
> >>"//*[not( ancestor-or-self::ContactInfo ) ]"
> >>Try that out and see what you get.
> >>
> >>Enjoy!
> >>Dave
> >>
> >>
> >>Pae Choi wrote:
> >>
> >>>Dave,
> >>>
> >>>It is partially working though, that was a nice XPath you gave. I
> >>>tested both Jaxen with DOM4J and XPE(seems like XPE also uses
> >>>Jaxen as well), the result was same as follows:
> >>>
> >>><Age>38</Age>
> >>><Gender>Female</Gender>
> >>>
> >>>It returns other sibing nodes excluding the <ContactInfo> element.
> >>>That's I wanted to see in that level, but I also need the other
> >>>elements, including their parent node, e.g., <Patient> element. To
> >>>wit, the result should be as follows:
> >>>
> >>><Patient>
> >>>    <Age>38</Age>
> >>>    <Gender>Female</Gender>
> >>></Patient>
> >>>
> >>>Any more comments? Thank you.
> >>>
> >>>Regards,
> >>>
> >>>
> >>>Pae
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>----- Original Message -----
> >>>From: "David D. Lucas" <[EMAIL PROTECTED]>
> >>>To: "Pae Choi" <[EMAIL PROTECTED]>
> >>>Cc: "Benjamin Kopic" <[EMAIL PROTECTED]>;
> >>><[EMAIL PROTECTED]>
> >>>Sent: Sunday, November 17, 2002 5:37 PM
> >>>Subject: Re: [dom4j-user] XPath result from a DOM and the Rest of DOM
> >>>
> >>>
> >>>
> >>>
> >>>>Try the XPath syntax "/*/*[local-name()!='ContactInfo']" .
> >>>>That should give you what you want.  I verified it with XPE, but have
> >>>>not tried it on DOM4J.
> >>>>
> >>>>Let me know if it works.  :-)
> >>>>Later,
> >>>>Dave
> >>>>
> >>>>
> >>>>Pae Choi wrote:
> >>>>
> >>>>
> >>>>>Thank you for your reply as well as code snippet. But that's not
> >>>>
> >>>exactlly
> >>>
> >>>
> >>>>>what I was looking for.
> >>>>>
> >>>>>My applogies if I did not explained well in my prevous message. The
> >>>>>code snippet in your message will be able to get the <ContactInfo>
> >>>>>subemelement fine. And I have no problem to getting that.
> >>>>>
> >>>>>My question was to get the rest of XML DOM, excluding the prviously
> >>>>>selected sublelement, <ContactInfo>. To wit, I am looking for a
simple
> >>>>>way to get the result without parsing the XML document by excluding
> >>>>>the previously selected subelement. The result I am expecting to get
> >>>>>is as follows:
> >>>>>
> >>>>>Initial XML:
> >>>>>/<Patient>/
> >>>>>/    <Age>39</Age>///
> >>>>>/    <Gender>Female</Gender>///
> >>>>>/    <ContactInfo>///
> >>>>>/        <PhoneNumber>123-456-7890</PhoneNumber>///
> >>>>>/        <EmailAddress>[EMAIL PROTECTED]</EmailAddress>///
> >>>>>/    </ContactInfo>///
> >>>>>/</Patient>///
> >>>>>The result XML:
> >>>>>/<Patient>/
> >>>>>/    <Age>39</Age>///
> >>>>>/    <Gender>Female</Gender>///
> >>>>>/</Patient>///
> >>>>>Thank you.
> >>>>>
> >>>>>Any more comments?
> >>>>>
> >>>>>Regards,
> >>>>>
> >>>>>
> >>>>>Pae
> >>>>>
> >>>>>
> >>>>>
> >>>>>   ----- Original Message -----
> >>>>>   *From:* Benjamin Kopic <mailto:[EMAIL PROTECTED]>
> >>>>>   *To:* Pae Choi <mailto:[EMAIL PROTECTED]>
> >>>>>   *Cc:* [EMAIL PROTECTED]
> >>>>>   <mailto:[EMAIL PROTECTED]>
> >>>>>   *Sent:* Sunday, November 17, 2002 4:07 AM
> >>>>>   *Subject:* Re: [dom4j-user] XPath result from a DOM and the Rest
of
> >>>>
> >>>DOM
> >>>
> >>>
> >>>>>   If I understand you correctly, the following will return <Patient>
> >>>>>   element:
> >>>>>
> >>>>>
> >>>>>   SAXReader reader = new SAXReader();
> >>>>>   Document document = reader.read( new File(
> >>>>
> >>>"patient_records.xml" ) );
> >>>
> >>>
> >>>>>   Element contactInfo = document.selectSingleNode( "//ContactInfo",
> >>>>>   "." );
> >>>>>   Element patient = contactInfor.getParent();
> >>>>>
> >>>>>
> >>>>>   Then you can use the Node API to traverse the <Patient> element
> >>>>>   returned.
> >>>>>
> >>>>>   Also, if <Patient> is the root element, then you can do the
> >>>>
> >>>following:
> >>>
> >>>
> >>>>>   SAXReader reader = new SAXReader();
> >>>>>   Document document = reader.read( new File(
> >>>>
> >>>"patient_records.xml" ) );
> >>>
> >>>
> >>>>>   Element patient = document.getRootElement();
> >>>>>
> >>>>>
> >>>>>
> >>>>>   Best regards
> >>>>>
> >>>>>   Ben
> >>>>>
> >>>>>
> >>>>>   On Sat, 2002-11-16 at 23:52, Pae Choi wrote:
> >>>>>
> >>>>>       /Say we have an XML document as follows:///
> >>>>>       ////
> >>>>>       /<Patient>///
> >>>>>       /    <Age>39</Age>///
> >>>>>       /    <Gender>Female</Gender>///
> >>>>>       /    <ContactInfo>///
> >>>>>       /        <PhoneNumber>123-456-7890</PhoneNumber>///
> >>>>>       /        <EmailAddress>[EMAIL PROTECTED]</EmailAddress>///
> >>>>>       /    </ContactInfo>///
> >>>>>       /</Patient>///
> >>>>>       ////
> >>>>>       /And we use the XPath to only get the <ContactInfo>, e.g.,///
> >>>>>       /"//ContactInfo" which should get the result as follows;///
> >>>>>       ////
> >>>>>       /    <ContactInfo>///
> >>>>>       /        <PhoneNumber>123-456-7890</PhoneNumber>///
> >>>>>       /        <EmailAddress>[EMAIL PROTECTED]</EmailAddress>///
> >>>>>       /    </ContactInfo>///
> >>>>>       ////
> >>>>>       /My question is:///
> >>>>>       ////
> >>>>>       /[Q] Is there way to get the rest of XML document in a
> >>>>
> > simple///
> >
> >>>>>       /      way. The rest of XML document should be as follows;///
> >>>>>       ////
> >>>>>       /<Patient>///
> >>>>>       /    <Age>39</Age>///
> >>>>>       /    <Gender>Female</Gender>///
> >>>>>       /</Patient>///
> >>>>>       ////
> >>>>>       /Any comments? Thank you.///
> >>>>>       ////
> >>>>>       /Regards,///
> >>>>>       ////
> >>>>>       ////
> >>>>>       /Pae///
> >>>>>       ////
> >>>>>       ////
> >>>>>       ////
> >>>>>
> >>>>>--
> >>>>>benjamin kopic
> >>>>>m: +44 (0)780 154 7643
> >>>>>t: +44 (0)20 7794 3090
> >>>>>e: [EMAIL PROTECTED]
> >>>>>w: http://www.panContext.com/
> >>>>>
> >>>>
> >>>>
> >>>>--
> >>>>
> >>>>+------------------------------------------------------------+
> >>>>| David Lucas                        mailto:[EMAIL PROTECTED]  |
> >>>>| Lucas Software Engineering, Inc.   (740) 964-6248 Voice    |
> >>>>| Unix,Java,C++,CORBA,XML,EJB        (614) 668-4020 Mobile   |
> >>>>| Middleware,Frameworks              (888) 866-4728 Fax/Msg  |
> >>>>+------------------------------------------------------------+
> >>>>| GPS Location:  40.0150 deg Lat,  -82.6378 deg Long         |
> >>>>| IMHC: "Jesus Christ is the way, the truth, and the life."  |
> >>>>| IMHC: "I know where I am; I know where I'm going."    <><  |
> >>>>+------------------------------------------------------------+
> >>>>
> >>>>Notes: PGP Key Block=http://www.lse.com/~ddlucas/pgpblock.txt
> >>>>IMHO="in my humble opinion" IMHC="in my humble conviction"
> >>>>All trademarks above are those of their respective owners.
> >>>>
> >>>>
> >>>
> >>>
> >>
> >>--
> >>
> >>+------------------------------------------------------------+
> >>| David Lucas                        mailto:[EMAIL PROTECTED]  |
> >>| Lucas Software Engineering, Inc.   (740) 964-6248 Voice    |
> >>| Unix,Java,C++,CORBA,XML,EJB        (614) 668-4020 Mobile   |
> >>| Middleware,Frameworks              (888) 866-4728 Fax/Msg  |
> >>+------------------------------------------------------------+
> >>| GPS Location:  40.0150 deg Lat,  -82.6378 deg Long         |
> >>| IMHC: "Jesus Christ is the way, the truth, and the life."  |
> >>| IMHC: "I know where I am; I know where I'm going."    <><  |
> >>+------------------------------------------------------------+
> >>
> >>Notes: PGP Key Block=http://www.lse.com/~ddlucas/pgpblock.txt
> >>IMHO="in my humble opinion" IMHC="in my humble conviction"
> >>All trademarks above are those of their respective owners.
> >>
> >>
> >>
> >>
> >>-------------------------------------------------------
> >>This sf.net email is sponsored by: To learn the basics of securing
> >>your web site with SSL, click here to get a FREE TRIAL of a Thawte
> >>Server Certificate: http://www.gothawte.com/rd524.html
> >>_______________________________________________
> >>dom4j-user mailing list
> >>[EMAIL PROTECTED]
> >>https://lists.sourceforge.net/lists/listinfo/dom4j-user
> >
> >
>
>
> --
>
> +------------------------------------------------------------+
> | David Lucas                        mailto:[EMAIL PROTECTED]  |
> | Lucas Software Engineering, Inc.   (740) 964-6248 Voice    |
> | Unix,Java,C++,CORBA,XML,EJB        (614) 668-4020 Mobile   |
> | Middleware,Frameworks              (888) 866-4728 Fax/Msg  |
> +------------------------------------------------------------+
> | GPS Location:  40.0150 deg Lat,  -82.6378 deg Long         |
> | IMHC: "Jesus Christ is the way, the truth, and the life."  |
> | IMHC: "I know where I am; I know where I'm going."    <><  |
> +------------------------------------------------------------+
>
> Notes: PGP Key Block=http://www.lse.com/~ddlucas/pgpblock.txt
> IMHO="in my humble opinion" IMHC="in my humble conviction"
> All trademarks above are those of their respective owners.
>
>



-------------------------------------------------------
This sf.net email is sponsored by: To learn the basics of securing 
your web site with SSL, click here to get a FREE TRIAL of a Thawte 
Server Certificate: http://www.gothawte.com/rd524.html
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to