velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Geir Magnusson Jr." <ge...@optonline.net>
Subject Re: wrapping XML document as JavaBeans?
Date Fri, 05 Oct 2001 13:26:23 GMT
On 10/2/01 9:04 PM, "Mike Williams" <mikew@cortexebusiness.com.au> wrote:

> Say I have the following XML document:
> 
>  <bean>
>    <name>UserInfo</name>
>    <field>
>      <name>id</name>
>      <type>int</type>
>    </field>
>    <field>
>      <name>name</name>
>      <type>java.lang.String</type>
>    </field>
>  </bean>     
> 
> I want to make the data therein available to a Velocity template.  I could
> do what Anakia does, and simply place a JDOM representation in the
> Context.  However, then I'd end up with template expressions like
> 
> $field.getChild("name").getText()
> 
> when I'd much rather write:
> 
> $field.name
> 
> I could create explicit model JavaBean objects, like Torque does, but I
> might want to deal with lots of different XML documents, and don't want to
> have to generate a set of JavaBeans for each DTD.
> 
> Are there any other options?  Would it be feasible to create a generic
> wrapper-ojbect that allows <field><name>id</name></field> to
be accessed as
> "$field.name" ?

I just upladed to /whiteboard/geir a jar xmleb.jar that contains an
example/starter of a tool like this.

It's pretty simple, but lets to take XML like

<?xml version="1.0" ?>
<!DOCTYPE document >

<document>
  
  <email>geirm@apache.org</email>

  <people>
    <person gender="male">
      <name>geir</name>
      <height>6'1"</height>
    </person>
    <person gender="female">
      <name>garbis</name>
      <height>7"</height>
    </person>
  </people>
</document>

( Garbis is my cat... She believes she is a person.)

And access in a template like

Email : $xml.email

#foreach($person in $xml.people.person)
   Name = $person.name ($person.getAttribute('gender') )
   $person.height
#end


For output like

Email : geirm@apache.org

   Name = geir (male )
   6'1"
   Name = garbis (female )
   7"


There are a whole bunch of things I don't really like about it, but figured
I would toss out here to see what happens...

If we like it, we can put in the contrib section.

geir

 
-- 
Geir Magnusson Jr.     geirm@optonline.net
System and Software Consulting
"Whoever would overthrow the liberty of a nation must begin by subduing the
freeness of speech." - Benjamin Franklin



Mime
View raw message