You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Michael Goldberg <MG...@yet2.com> on 2000/12/29 18:40:37 UTC

RE: Parsing a document and writing out a specfic node to a new fi le

All,

First, thanks to Christopher and Paul for the prompt responses to my
request.

I tried using the XMLSerializer technique mentioned by Paul and shown in the
DOMGenerate example sent by Christopher.  I'm getting closer, but I'm still
not there yet.  I am getting a "Wrong document" error (WRONG_DOCUMENT_ERR)
on appendChild because the child node that I am appending to my new document
comes from a different document.

Looking at the DOMGenerate example a little closer, I see that all of the
nodes that are placed into the new document via appendChild are always new
nodes, not nodes from another document.

Any suggestion on how to get around this latest twist?  Is there something I
can do when creating the new DocumentImpl, perhaps telling the constructor
to create a new document based on an existing one?  Maybe I don't need to
create a document at all?  Is there a way to serialize just a node instead
of an entire document?  I see there is a serializeNode() method, but it
protected, so I don't think I can use it.

Again, any help would be greatly appreciated!

Here's my code (mostly stolen from DOMGenerate).  "divDetailedDescription"
is the node I am trying to print to a file.

    Document outDoc= new DocumentImpl();
    Element root = outDoc.createElement( "Section2" );     // Create Root
Element
    root.appendChild( divDetailedDescription );            // Attach Element
as a child of root

    outDoc.appendChild( root );                            // Add Root to
Document

    OutputFormat    format  = new OutputFormat( outDoc );   //Serialize DOM
    XMLSerializer    serial = new XMLSerializer( outStream, format );
    serial.asDOMSerializer();                            // As a DOM
Serializer

    serial.serialize( outDoc.getDocumentElement() );

Thanks,
Michael

 -----Original Message-----
From: 	Sellers, Christopher [mailto:christopher.sellers@wilcom.com] 
Sent:	Friday, December 29, 2000 6:34 AM
To:	'xerces-j-dev@xml.apache.org'
Subject:	RE: Parsing a document and writing out a specfic node to a
new fi le

 << File: NodePrintSample.txt >> << File: DOMGenerate.java >> << File:
ATT08090.txt >> Here's kind of a brute force approach.  I've also attached a
sample included
with Xerces that shows the more eloquent solution proposed by Paul Brown.

Christopher Sellers
(918) 573-5186
Williams Communications



-----Original Message-----
From: Michael Goldberg [mailto:MGoldberg@yet2.com]
Sent: Thursday, December 28, 2000 6:06 PM
To: 'xerces-j-dev@xml.apache.org'
Subject: Parsing a document and writing out a specfic node to a new file


All,

I am in desperate need of some sample code that writes a node to a file.
More specifically, I am provided with an XHTML file as input.  I need to
scan/parse the document for a specific node.  Once found, I need to create a
second XHTML file which contains just this node (and all of its child
nodes).

I think I have the first part down.  I parse the input file as a Document
and use various methods like getDocumentElement(), getLastChild(), and
getChildNodes() to get to the specific node I am looking for.  However, now
that I have a node, I don't know how to create a file and output all of the
node's contents into the file.

Can please someone suggest some classes and methods to look into?  Even
better would be some sample code!

Sincerely,
Michael S. Goldberg
mgoldberg@yet2.com

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


Re: Parsing a document and writing out a specfic node to a new file

Posted by Gary Pennington <Ga...@uk.sun.com>.
Hi Michael,

You need to use importNode not appendChild when moving nodes between documents.

Gary

Michael Goldberg wrote:

> All,
>
> First, thanks to Christopher and Paul for the prompt responses to my
> request.
>
> I tried using the XMLSerializer technique mentioned by Paul and shown in the
> DOMGenerate example sent by Christopher.  I'm getting closer, but I'm still
> not there yet.  I am getting a "Wrong document" error (WRONG_DOCUMENT_ERR)
> on appendChild because the child node that I am appending to my new document
> comes from a different document.
>
> Looking at the DOMGenerate example a little closer, I see that all of the
> nodes that are placed into the new document via appendChild are always new
> nodes, not nodes from another document.
>
> Any suggestion on how to get around this latest twist?  Is there something I
> can do when creating the new DocumentImpl, perhaps telling the constructor
> to create a new document based on an existing one?  Maybe I don't need to
> create a document at all?  Is there a way to serialize just a node instead
> of an entire document?  I see there is a serializeNode() method, but it
> protected, so I don't think I can use it.
>
> Again, any help would be greatly appreciated!
>
> Here's my code (mostly stolen from DOMGenerate).  "divDetailedDescription"
> is the node I am trying to print to a file.
>
>     Document outDoc= new DocumentImpl();
>     Element root = outDoc.createElement( "Section2" );     // Create Root
> Element
>     root.appendChild( divDetailedDescription );            // Attach Element
> as a child of root
>
>     outDoc.appendChild( root );                            // Add Root to
> Document
>
>     OutputFormat    format  = new OutputFormat( outDoc );   //Serialize DOM
>     XMLSerializer    serial = new XMLSerializer( outStream, format );
>     serial.asDOMSerializer();                            // As a DOM
> Serializer
>
>     serial.serialize( outDoc.getDocumentElement() );
>
> Thanks,
> Michael
>
>  -----Original Message-----
> From:   Sellers, Christopher [mailto:christopher.sellers@wilcom.com]
> Sent:   Friday, December 29, 2000 6:34 AM
> To:     'xerces-j-dev@xml.apache.org'
> Subject:        RE: Parsing a document and writing out a specfic node to a
> new fi le
>
>  << File: NodePrintSample.txt >> << File: DOMGenerate.java >> << File:
> ATT08090.txt >> Here's kind of a brute force approach.  I've also attached a
> sample included
> with Xerces that shows the more eloquent solution proposed by Paul Brown.
>
> Christopher Sellers
> (918) 573-5186
> Williams Communications
>
> -----Original Message-----
> From: Michael Goldberg [mailto:MGoldberg@yet2.com]
> Sent: Thursday, December 28, 2000 6:06 PM
> To: 'xerces-j-dev@xml.apache.org'
> Subject: Parsing a document and writing out a specfic node to a new file
>
> All,
>
> I am in desperate need of some sample code that writes a node to a file.
> More specifically, I am provided with an XHTML file as input.  I need to
> scan/parse the document for a specific node.  Once found, I need to create a
> second XHTML file which contains just this node (and all of its child
> nodes).
>
> I think I have the first part down.  I parse the input file as a Document
> and use various methods like getDocumentElement(), getLastChild(), and
> getChildNodes() to get to the specific node I am looking for.  However, now
> that I have a node, I don't know how to create a file and output all of the
> node's contents into the file.
>
> Can please someone suggest some classes and methods to look into?  Even
> better would be some sample code!
>
> Sincerely,
> Michael S. Goldberg
> mgoldberg@yet2.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org