You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Juergen Weber <we...@yahoo.com> on 2004/04/29 19:31:20 UTC

[digester] how to parse node trees

Hi,

I want to parse an xml for this dtd


<!ELEMENT node (node*) >
<!ELEMENT node (#PCDATA) >
<!ATTLIST node type CDATA "name">

like

<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE node SYSTEM "tree.dtd">
<node name="Impulsdefinitionen">
	<node name="a">
		<node name="b">
			<node name="c">
				<node name="d">
				</node>
			</node>
			<node name="e">
			</node>
		</node>
	</node>
</node>

I tried 

Digester digester = new Digester();
digester.setValidating(false);

digester.addObjectCreate("node",Node.class);
digester.addSetNext("*/node", "addNode");

InputSource inputSource = new InputSource(xmlfile);

inputSource.setSystemId(dtdfile.toString());

return digester.parse(inputSource);

but this failed with

29.04.2004 19:22:09
org.apache.commons.digester.Digester peek
WARNUNG: Empty stack (returning null)
29.04.2004 19:22:09
org.apache.commons.digester.Digester endElement
SCHWERWIEGEND: End event threw exception
java.lang.NullPointerException
	at
org.apache.commons.beanutils.MethodUtils.invokeMethod(MethodUtils.java:245)
	
	
	
What did I forget?

Node is

public class Node
{
	List l = new ArrayList();
	String name;
	
	public void addNode(Node node)
	{
		l.add(node);
	}
	
	public Iterator getNodes()
	{
		return l.iterator();
	}
	
	public String getName()
	{
		return name;
	}
	
    public void setName(String name)
	{
		this.name = name;
	}
}

Thanks, Juergen



	
		
__________________________________
Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs  
http://hotjobs.sweepstakes.yahoo.com/careermakeover 

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


Re: [digester] how to parse node trees

Posted by Simon Kitching <si...@ecnetwork.co.nz>.
On Fri, 2004-04-30 at 05:51, José Antonio Pérez Testa wrote:
> Hi Juergen,
> apart from corrections donwside, you have to push an object to the 
> Digester stack 
[...]
> That is because the SetNext rule pops the element from the stack, so if  
> you start up with nothing you end up with nothing.

Hi Juergen,

Jose is right; if you have <node> as the root element of your document,
and a SetNextRule with pattern "*/node" then when that rule fires it
will "peek" at the second-to-top element on the object stack, and try to
call the appropriate method on it passing the top object. But there
won't be a second-to-top object so an exception will be generated. 

SetNextRule doesn't actually pop anything from the stack, it just peeks
at the top and second-to-top objects. But when the stack is only one
object deep....

The easiest solution for this is (as Jose says) to manually push an
object onto the stack before parsing starts. This object must have an
addNode(Node) method - which probably means simply pushing a Node object
onto the stack to behave as the "root" of the generated tree.

An alternative is to create "special case" rules so that the root
element is treated differently (avoid the SetNextRule). Because rules
without wildcards take precedence over rules with wildcards this isn't
too hard to do (see the RulesBase javadoc for more info).

> >
> //digester.addObjectCreate("node",Node.class);
> digester.addObjectCreate("*/node",Node.class);

Yep, I second Jose on this too.


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] how to parse node trees

Posted by José Antonio Pérez Testa <ja...@indra.es>.
Hi Juergen,
apart from corrections donwside, you have to push an object to the 
Digester stack in order to have something to return when
the parsing of the xml ends.
That is because the SetNext rule pops the element from the stack, so if  
you start up with nothing you end up with nothing.

Simon surely could give a better explanation but i think that is the 
basic idea!


Juergen Weber wrote:

>Hi,
>
>I want to parse an xml for this dtd
>
>
><!ELEMENT node (node*) >
><!ELEMENT node (#PCDATA) >
><!ATTLIST node type CDATA "name">
>
>like
>
><?xml version="1.0" encoding="iso-8859-1"?>
>
><!DOCTYPE node SYSTEM "tree.dtd">
><node name="Impulsdefinitionen">
>	<node name="a">
>		<node name="b">
>			<node name="c">
>				<node name="d">
>				</node>
>			</node>
>			<node name="e">
>			</node>
>		</node>
>	</node>
></node>
>
>I tried 
>
>Digester digester = new Digester();
>digester.setValidating(false);
>  
>
//digester.addObjectCreate("node",Node.class);
digester.addObjectCreate("*/node",Node.class);
digester.|addSetProperties(|"*/node",|"name"||, "name");|

>digester.addSetNext("*/node", "addNode");
>
>InputSource inputSource = new InputSource(xmlfile);
>
>inputSource.setSystemId(dtdfile.toString());
>
>return digester.parse(inputSource);
>
>but this failed with
>
>29.04.2004 19:22:09
>org.apache.commons.digester.Digester peek
>WARNUNG: Empty stack (returning null)
>29.04.2004 19:22:09
>org.apache.commons.digester.Digester endElement
>SCHWERWIEGEND: End event threw exception
>java.lang.NullPointerException
>	at
>org.apache.commons.beanutils.MethodUtils.invokeMethod(MethodUtils.java:245)
>	
>	
>	
>What did I forget?
>
>Node is
>
>public class Node
>{
>	List l = new ArrayList();
>	String name;
>	
>	public void addNode(Node node)
>	{
>		l.add(node);
>	}
>	
>	public Iterator getNodes()
>	{
>		return l.iterator();
>	}
>	
>	public String getName()
>	{
>		return name;
>	}
>	
>    public void setName(String name)
>	{
>		this.name = name;
>	}
>}
>
>Thanks, Juergen
>
>
>
>	
>		
>__________________________________
>Do you Yahoo!?
>Win a $20,000 Career Makeover at Yahoo! HotJobs  
>http://hotjobs.sweepstakes.yahoo.com/careermakeover 
>
>---------------------------------------------------------------------
>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