commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jörg Schaible <Joerg.Schai...@scalaris.com>
Subject RE: parse xml with digester
Date Wed, 29 Oct 2008 07:05:55 GMT

Peter_Ford@blm.gov wrote:
> From experience I find Betwixt easier than Digester for this,
> because it
> handles the Digester rules for you. Also once you've set up
> your mappings
> and strategy objects (which isn't difficult) you get Object-to-XML and
> XML-to-Object at the same time. In practise I can set up my Betwixt
> configuration and test it by creating an object of the
> relevant class and
> dumping it as XML; once I know it generates XML in the form I
> need, I can
> then use the same configuration to read the XML without modification.

[snip]

>>>>>> I have an XML document like follows:
>>>>>> 
>>>>>> <item att1="" att2="">
>>>>>>    <item att1="" att2="">
>>>>>>       <item att1="" att2="">
>>>>>>       </item>
>>>>>>    </item>
>>>>>>    <item att1="" att2="">
>>>>>>    </item>
>>>>>> </item>
>>>>>> 
>>>>>> Simply a nested structure of one complex element having two
>>>>>> required attributes. 
>>>>>> 
>>>>>> I write a POJO that match this structure, what code
> would read the
> xml
>>>>>> document and parse into the Java object?

An alternative could have been XStream:

======== %< ===========
class Item {
    List items;
    String attr1;
    String attr2;
}

XStream xstream = new XStream();
xstream.alias("item", Item.class);
xstream.useAttributeFor(String.class);
xstream.addImplicitCollection(Item.class, "items", "item", Item.class);
Item item = (Item)xstream.fromXML(xml);
======== %< ===========

;-)

- Jörg

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


Mime
View raw message