You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Peter Pimley <pp...@semantico.com> on 2004/09/13 16:47:32 UTC

[digester] noob failing to pull in raw XML

Hello everybody.

I'd like to use digester to parse an XML file.  What makes my situation 
unusual is that sometimes I want to be able to pull in raw XML withing 
trying to interpret it.  My documents are of the form:

<entries>
  <entry>(raw XML data)</entry>
  <entry>(even more data)</entry>
  etc....


All I know about the raw XML data is that it is gaurenteed -not- to 
contain an </entry> tag.  Other than that, your guess is as good as 
mine, as it comes from the users of my system.  It might not even be 
valid XML.  So, I just want to read it in as completely raw data up 
until the end tag.

My first attempt (I've never used digester before) was to add something 
like:

digester.addCallMethod ("entries/entry" "doStuff");

... but this didn't work.  Typically, the raw XML starts with some start 
tag of its own, so the <entry> tag has an empty body.  The String passed 
into "doStuff" has zero length.

Is there a way to tell digester to ignore all XML tags from a certain 
node downwards?

Thanks in advance,
Peter Pimley, Semantico


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [digester] noob failing to pull in raw XML

Posted by Peter Pimley <pp...@semantico.com>.
José Antonio Pérez Testa wrote:

> You can preprocess your input xml and put in  CDATA tags and pass the 
> new xml to Digester!
> <entries>
> <entry><![CDATA[(raw XML data)]]></entry>
> <entry><![CDATA[(even more data)]]</entry>
> etc....


Of course!  This is definately what I need to do.  :)  Thanks for the 
replies.


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [digester] noob failing to pull in raw XML

Posted by José Antonio Pérez Testa <ja...@indra.es>.
You can preprocess your input xml and put in  CDATA tags and pass the 
new xml to Digester!
<entries>
 <entry><![CDATA[(raw XML data)]]></entry>
 <entry><![CDATA[(even more data)]]</entry>
 etc....

Regards.
Peter Pimley wrote:

>
> Hello everybody.
>
> I'd like to use digester to parse an XML file.  What makes my 
> situation unusual is that sometimes I want to be able to pull in raw 
> XML withing trying to interpret it.  My documents are of the form:
>
> <entries>
>  <entry>(raw XML data)</entry>
>  <entry>(even more data)</entry>
>  etc....
>
>
> All I know about the raw XML data is that it is gaurenteed -not- to 
> contain an </entry> tag.  Other than that, your guess is as good as 
> mine, as it comes from the users of my system.  It might not even be 
> valid XML.  So, I just want to read it in as completely raw data up 
> until the end tag.
>
> My first attempt (I've never used digester before) was to add 
> something like:
>
> digester.addCallMethod ("entries/entry" "doStuff");
>
> ... but this didn't work.  Typically, the raw XML starts with some 
> start tag of its own, so the <entry> tag has an empty body.  The 
> String passed into "doStuff" has zero length.
>
> Is there a way to tell digester to ignore all XML tags from a certain 
> node downwards?
>
> Thanks in advance,
> Peter Pimley, Semantico
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
-------------------------------------------------------------------------------------------------------------------
Este correo electrónico y, en su caso, cualquier fichero anexo al mismo, contiene información de carácter confidencial exclusivamente dirigida a su destinatario o destinatarios. Queda prohibida su divulgación, copia o distribución a terceros sin la previa autorización escrita de Indra. En el caso de haber recibido este correo electrónico por error, se ruega notificar inmediatamente esta circunstancia mediante reenvío a la dirección electrónica del remitente.

The information in this e-mail and in any attachments is confidential and solely for the attention and use of the named addressee(s). You are hereby notified that any dissemination, distribution or copy of this communication is prohibited without the prior written consent of Indra. If you have received this communication in error, please, notify the sender by reply e-mail

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [digester] noob failing to pull in raw XML

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
On Tue, 2004-09-14 at 05:43, Wade Chandler wrote:
> Peter Pimley wrote:
> 
> > 
> > Hello everybody.
> > 
> > I'd like to use digester to parse an XML file.  What makes my situation 
> > unusual is that sometimes I want to be able to pull in raw XML withing 
> > trying to interpret it.  My documents are of the form:
> > 
> > <entries>
> >  <entry>(raw XML data)</entry>
> >  <entry>(even more data)</entry>
> >  etc....
> > 
> > 
> > All I know about the raw XML data is that it is gaurenteed -not- to 
> > contain an </entry> tag.  Other than that, your guess is as good as 
> > mine, as it comes from the users of my system.  It might not even be 
> > valid XML.  So, I just want to read it in as completely raw data up 
> > until the end tag.
> > 
> > My first attempt (I've never used digester before) was to add something 
> > like:
> > 
> > digester.addCallMethod ("entries/entry" "doStuff");
> > 
> > ... but this didn't work.  Typically, the raw XML starts with some start 
> > tag of its own, so the <entry> tag has an empty body.  The String passed 
> > into "doStuff" has zero length.
> > 
> > Is there a way to tell digester to ignore all XML tags from a certain 
> > node downwards?
> > 
> > Thanks in advance,
> > Peter Pimley, Semantico
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> > 
> > 
> > 
> 
> Use a CDATA section in your XML.

Yes, if the content of the entry tag may not be valid XML, then a CDATA
section is your only option. Digester is simply a handler for SAX events
generated by an XML parser; the parser won't process the input if it
isn't valid XML and there is nothing that can be done in Digester about
that. 

And there is no xml parser I know of that can be told to "stop parsing
and just return raw text" when a certain tag is encountered. You might
possibly be able to do this by creating the parser yourself using an
InputSource you have created, then creating a custom Digester rule which
reads direct from that input source when fired, until the </entry>
string is found. The effect would be that the parser would essentially
never see the content of the <entry> tag. Tricky, though, particularly
if the parser uses "read-ahead" caching or similar.

Using CDATA tags is much cleaner if you can arrange for the input to
have them; content is just "text" contained in the enclosing element, so
can be handled using the usual digester rules.

If the input *is* valid XML then you may wish to look at the
NodeCreateRule digester rule class. It builds a DOM document fragment
from the input. This may or may not be what you want. There isn't a
factory method on the Digester class for NodeCreateRule because it's not
a commonly-used rule; you need to create it directly:

  NodeCreateRule ncr = new NodeCreateRule();
  digester.addRule("entries/entry", ncr);

  // and tell some parent object about the DOM object created 
  // from the entry node
  digester.addSetNext("entries/entry", "addDOMTree");

Regards,

Simon



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [digester] noob failing to pull in raw XML

Posted by Wade Chandler <wc...@redesetgrow.com>.
Peter Pimley wrote:

> 
> Hello everybody.
> 
> I'd like to use digester to parse an XML file.  What makes my situation 
> unusual is that sometimes I want to be able to pull in raw XML withing 
> trying to interpret it.  My documents are of the form:
> 
> <entries>
>  <entry>(raw XML data)</entry>
>  <entry>(even more data)</entry>
>  etc....
> 
> 
> All I know about the raw XML data is that it is gaurenteed -not- to 
> contain an </entry> tag.  Other than that, your guess is as good as 
> mine, as it comes from the users of my system.  It might not even be 
> valid XML.  So, I just want to read it in as completely raw data up 
> until the end tag.
> 
> My first attempt (I've never used digester before) was to add something 
> like:
> 
> digester.addCallMethod ("entries/entry" "doStuff");
> 
> ... but this didn't work.  Typically, the raw XML starts with some start 
> tag of its own, so the <entry> tag has an empty body.  The String passed 
> into "doStuff" has zero length.
> 
> Is there a way to tell digester to ignore all XML tags from a certain 
> node downwards?
> 
> Thanks in advance,
> Peter Pimley, Semantico
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 
> 

Use a CDATA section in your XML.

Wade


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [digester] noob failing to pull in raw XML

Posted by Reid Pinchback <re...@yahoo.com>.
Two cases:

1. your "raw data" could be a complete XML doc.

2. your "raw data" will never be an XML doc,
   only a well-formed XML element tree.

In the first case, the solution is to wrap
your "raw xml" in a CDATA section, then
digester rules will just treat it as body text.  
>From what you describe, chances are you should be
doing that anyways so xml processing declarations
don't cause you problems.

In the second case the solution is to write the
patterns for your rules such that you'll never
bother acting on a particular subtree.  You'll
still parse it, just not process any rules.


--- Peter Pimley <pp...@semantico.com> wrote:

> 
> Hello everybody.
> 
> I'd like to use digester to parse an XML file.  What
> makes my situation 
> unusual is that sometimes I want to be able to pull
> in raw XML withing 
> trying to interpret it.  My documents are of the
> form:
> 
> <entries>
>   <entry>(raw XML data)</entry>
>   <entry>(even more data)</entry>
>   etc....
> 
> 
> All I know about the raw XML data is that it is
> gaurenteed -not- to 
> contain an </entry> tag.  Other than that, your
> guess is as good as 
> mine, as it comes from the users of my system.  It
> might not even be 
> valid XML.  So, I just want to read it in as
> completely raw data up 
> until the end tag.
> 
> My first attempt (I've never used digester before)
> was to add something 
> like:
> 
> digester.addCallMethod ("entries/entry" "doStuff");
> 
> ... but this didn't work.  Typically, the raw XML
> starts with some start 
> tag of its own, so the <entry> tag has an empty
> body.  The String passed 
> into "doStuff" has zero length.
> 
> Is there a way to tell digester to ignore all XML
> tags from a certain 
> node downwards?
> 
> Thanks in advance,
> Peter Pimley, Semantico
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> commons-user-help@jakarta.apache.org
> 
> 



	
		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
http://promotions.yahoo.com/new_mail 

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org