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 Samuel Cheung <SC...@Novarra.com> on 2002/05/20 17:21:56 UTC

How to use Neko parser to parse Document fragment

Hi,

Could someone please tell me if I can use Neko HTML parser to parse Document
Fragment?
For example, if I have a string "<i> this is some text </i>" and an existing
document (owner document), can i use Neko to create an element (for the I
tag) with a text node as its child. And I can append this new Element to an
existing element of the owner document?

Thanks.
Sam

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


Re: How to use Neko parser to parse Document fragment

Posted by Andy Clark <an...@apache.org>.
SB wrote:
> > scanner's current entity stack. There is a sample in
> > the latest package that demonstrates this functionality.
> 
> I am not able to find this sample in Xerces-J-src-2.0.1.tar.gz
> that I downloaded last month. Can you tell me the name
> of this sample?

I was referring to the latest NekoHTML package. The sample 
is located in the src/sample/ directory in a file called
"Script.java". You would have to write a new filter to do
what you need but you can use this class as an example.

However, I am thinking of adding a filter that will handle
this kind of buffering of element content for you. My current
idea is to have a generic buffering filter where you could set
the element to buffer and then receive a callback with the
contents of the buffer at the close of that element.

-- 
Andy Clark * andyc@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


Re: How to use Neko parser to parse Document fragment

Posted by SB <st...@cyberspace.org>.
Quoting Andy Clark (andyc@apache.org):
> SB wrote:
> > there is no direct way to write in the middle of a HTML document. Is
> > there any trick or workaround with the existing HTML Dom parser
> > to add elements to HTML Document dynamically?
> 
> You could write a filter that buffers the content of a
> <script> element, calls a JavaScript engine, and then
> pushes a new character reader of the output onto the
> scanner's current entity stack. There is a sample in
> the latest package that demonstrates this functionality.

I am not able to find this sample in Xerces-J-src-2.0.1.tar.gz
that I downloaded last month. Can you tell me the name
of this sample?

st

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


Re: How to use Neko parser to parse Document fragment

Posted by Andy Clark <an...@apache.org>.
SB wrote:
> there is no direct way to write in the middle of a HTML document. Is
> there any trick or workaround with the existing HTML Dom parser
> to add elements to HTML Document dynamically?

You could write a filter that buffers the content of a
<script> element, calls a JavaScript engine, and then
pushes a new character reader of the output onto the
scanner's current entity stack. There is a sample in
the latest package that demonstrates this functionality.

If, however, you need to parse a document fragment but
not during the initial parse, then you're out of luck for 
the time being. Later I'll think about how best to add a
document fragment parser.

-- 
Andy Clark * andyc@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


Re: How to use Neko parser to parse Document fragment

Posted by SB <st...@cyberspace.org>.
Quoting Andy Clark (andyc@apache.org):
> Samuel Cheung wrote:
> > For example, if I have a string "<i> this is some text </i>" and an existing
> 
> This will be parsed and returned as if it were "<HTML><BODY><I>this 
> is some text</I></BODY></HTML>". There currently is no document
> "fragment" DOM parser that will return only those nodes that were
> scanned. However, it would be relatively easy to build one -- it
> just hasn't been done, yet.


This is very much needed to support Dynamic HTML. HTMLDocument.write
method seems to work only in conjunction with HTMLDocument.open, so 
there is no direct way to write in the middle of a HTML document. Is
there any trick or workaround with the existing HTML Dom parser
to add elements to HTML Document dynamically?

thanks,
st.

> 
> > document (owner document), can i use Neko to create an element (for the I
> > tag) with a text node as its child. And I can append this new Element to an
> > existing element of the owner document?
> 
> You'll be returned a different document so, just like any other
> DOM tree, you can't directly move nodes from one to the other.
> The standard way is to call "importNode" on the destination doc
> which makes a copy of the source tree in the new document.
> There's also a new method being added to the DOM called "adopt-
> Node" which will move nodes directly (assuming that the impls
> are identical).




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


Re: How to use Neko parser to parse Document fragment

Posted by SB <st...@cyberspace.org>.
Quoting Andy Clark (andyc@apache.org):
> Samuel Cheung wrote:
> > For example, if I have a string "<i> this is some text </i>" and an existing
> 
> This will be parsed and returned as if it were "<HTML><BODY><I>this 
> is some text</I></BODY></HTML>". There currently is no document
> "fragment" DOM parser that will return only those nodes that were
> scanned. However, it would be relatively easy to build one -- it
> just hasn't been done, yet.


This is very much needed to support Dynamic HTML. HTMLDocument.write
method seems to work only in conjunction with HTMLDocument.open, so 
there is no direct way to write in the middle of a HTML document. Is
there any trick or workaround with the existing HTML Dom parser
to add elements to HTML Document dynamically?

thanks,
st.

> 
> > document (owner document), can i use Neko to create an element (for the I
> > tag) with a text node as its child. And I can append this new Element to an
> > existing element of the owner document?
> 
> You'll be returned a different document so, just like any other
> DOM tree, you can't directly move nodes from one to the other.
> The standard way is to call "importNode" on the destination doc
> which makes a copy of the source tree in the new document.
> There's also a new method being added to the DOM called "adopt-
> Node" which will move nodes directly (assuming that the impls
> are identical).




---------------------------------------------------------------------
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 use Neko parser to parse Document fragment

Posted by Andy Clark <an...@apache.org>.
Samuel Cheung wrote:
> For example, if I have a string "<i> this is some text </i>" and an existing

This will be parsed and returned as if it were "<HTML><BODY><I>this 
is some text</I></BODY></HTML>". There currently is no document
"fragment" DOM parser that will return only those nodes that were
scanned. However, it would be relatively easy to build one -- it
just hasn't been done, yet.

> document (owner document), can i use Neko to create an element (for the I
> tag) with a text node as its child. And I can append this new Element to an
> existing element of the owner document?

You'll be returned a different document so, just like any other
DOM tree, you can't directly move nodes from one to the other.
The standard way is to call "importNode" on the destination doc
which makes a copy of the source tree in the new document.
There's also a new method being added to the DOM called "adopt-
Node" which will move nodes directly (assuming that the impls
are identical).

-- 
Andy Clark * andyc@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