You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Anthony Mak <an...@nicta.com.au> on 2005/02/10 01:29:01 UTC

How to get an xml subtree using DOM?

Hi all,

I am using Xerces DOM library. I want to get a subtree (everything 
including the tags ) from an xml document as a string ( and save this 
subtree into a xml/text file). Currently I am using the TreeWalkerImpl 
class and nextSibling() and getCurretnNode() to traverse the xml tree.

(The reason why I want to do this is I needed to get the subtree as an 
xml file to feel it to a Mathml to image converter program.)

Much appreciate if you can send me some codes or point me which methods 
I need to use. Thanks!!

Anthony
-- 
Anthony Mak
Research Programmer
Logic and Computation
National ICT Australia (Canberra)
http://www.nicta.com.au

"This email and any attachments are confidential. They may contain 
legally privileged information or copyright material. You should not 
read, copy, use or disclose them without authorisation.  If you are not 
an intended recipient, please contact us at once by return email and 
then delete both messages.  We do not accept liability in connection 
with computer virus, data corruption, delay, interruption, unauthorised 
access or unauthorised amendment.  This notice should not be removed"

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: How to get an xml subtree using DOM?

Posted by Jeff Greif <jg...@alumni.princeton.edu>.
see http://xml.apache.org/xerces2-j/faq-general.html#faq-5

----- Original Message ----- 
From: "Gaurav Gupta" <Ga...@Sun.COM>
To: <xe...@xml.apache.org>
Sent: Wednesday, February 23, 2005 10:37 PM
Subject: Re: How to get an xml subtree using DOM?


> I think this can be done by first reaching to the node whose subtree you 
> require and then serialize that subtree into the string.
> 
> Gaurav
> 
> Anthony Mak wrote:
> 
> > Hi all,
> >
> > I am using Xerces DOM library. I want to get a subtree (everything 
> > including the tags ) from an xml document as a string ( and save this 
> > subtree into a xml/text file). Currently I am using the TreeWalkerImpl 
> > class and nextSibling() and getCurretnNode() to traverse the xml tree.
> >
> > (The reason why I want to do this is I needed to get the subtree as an 
> > xml file to feel it to a Mathml to image converter program.)
> >
> > Much appreciate if you can send me some codes or point me which 
> > methods I need to use. Thanks!!
> >
> > Anthony
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: How to get an xml subtree using DOM?

Posted by Gaurav Gupta <Ga...@Sun.COM>.
I think this can be done by first reaching to the node whose subtree you 
require and then serialize that subtree into the string.

Gaurav

Anthony Mak wrote:

> Hi all,
>
> I am using Xerces DOM library. I want to get a subtree (everything 
> including the tags ) from an xml document as a string ( and save this 
> subtree into a xml/text file). Currently I am using the TreeWalkerImpl 
> class and nextSibling() and getCurretnNode() to traverse the xml tree.
>
> (The reason why I want to do this is I needed to get the subtree as an 
> xml file to feel it to a Mathml to image converter program.)
>
> Much appreciate if you can send me some codes or point me which 
> methods I need to use. Thanks!!
>
> Anthony



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


RE: How to get an xml subtree using DOM?

Posted by Rick Bullotta <ri...@lighthammer.com>.
Use a serializer such as in:

 

public static String outerXML(Node sourceNode) {

  try {

    Transformer outerXmlTransformer =
createTransformerFactory().newTransformer();

 

    outerXmlTransformer.setOutputProperty("omit-xml-declaration", "yes");

 

    DOMSource nodeSource = new DOMSource(sourceNode);

 

    StringWriter resultStringWriter = new StringWriter();

    StreamResult streamResult = new StreamResult(resultStringWriter);

 

    outerXmlTransformer.transform(nodeSource, streamResult);

 

    return resultStringWriter.toString();

  } catch (Exception ex) {

    return Constants.EMPTY_STRING;

  }

}

 

Rick Bullotta

CTO

Lighthammer Software (http://www.lighthammer.com)

 

 

-----Original Message-----
From: Anthony Mak [mailto:anthony.mak@nicta.com.au] 
Sent: Wednesday, February 09, 2005 7:29 PM
To: xerces-j-user@xml.apache.org
Subject: How to get an xml subtree using DOM?

 

Hi all,

 

I am using Xerces DOM library. I want to get a subtree (everything 

including the tags ) from an xml document as a string ( and save this 

subtree into a xml/text file). Currently I am using the TreeWalkerImpl 

class and nextSibling() and getCurretnNode() to traverse the xml tree.

 

(The reason why I want to do this is I needed to get the subtree as an 

xml file to feel it to a Mathml to image converter program.)

 

Much appreciate if you can send me some codes or point me which methods 

I need to use. Thanks!!

 

Anthony

-- 

Anthony Mak

Research Programmer

Logic and Computation

National ICT Australia (Canberra)

http://www.nicta.com.au

 

"This email and any attachments are confidential. They may contain 

legally privileged information or copyright material. You should not 

read, copy, use or disclose them without authorisation.  If you are not 

an intended recipient, please contact us at once by return email and 

then delete both messages.  We do not accept liability in connection 

with computer virus, data corruption, delay, interruption, unauthorised 

access or unauthorised amendment.  This notice should not be removed"

 

---------------------------------------------------------------------

To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org

For additional commands, e-mail: xerces-j-user-help@xml.apache.org