Return-Path: Delivered-To: apmail-jakarta-commons-user-archive@www.apache.org Received: (qmail 63322 invoked from network); 2 Mar 2004 16:49:43 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 2 Mar 2004 16:49:43 -0000 Received: (qmail 33332 invoked by uid 500); 2 Mar 2004 16:48:02 -0000 Delivered-To: apmail-jakarta-commons-user-archive@jakarta.apache.org Received: (qmail 33283 invoked by uid 500); 2 Mar 2004 16:48:02 -0000 Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Users List" Reply-To: "Jakarta Commons Users List" Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 33251 invoked from network); 2 Mar 2004 16:48:01 -0000 Received: from unknown (HELO usknausglvxrly2.kraft.com) (192.97.38.27) by daedalus.apache.org with SMTP; 2 Mar 2004 16:48:01 -0000 Received: from KFNASMTP03.kraft.com ([146.237.30.30]) by usknausglvxrly2.kraft.com with Microsoft SMTPSVC(5.0.2195.5329); Tue, 2 Mar 2004 10:49:03 -0600 Received: from kcdmexc0.kraft.com ([192.97.103.40]) by usknausglvimc01.kraft.com with SMTP (Microsoft Exchange Internet Mail Service Version 5.5.2656.59) id F7T8FT8R; Tue, 2 Mar 2004 10:48:06 -0600 Received: by KCDMEXC0 with Internet Mail Service (5.5.2653.19) id ; Tue, 2 Mar 2004 11:44:31 -0500 Message-ID: <2E3BB7FA184A06429ED18F7AB7E92C94086EA0E7@kcdmexc2> From: "Savitsky, Alex" To: 'Jakarta Commons Users List' Subject: RE: [Digester] Parsing XML to a hashtable Date: Tue, 2 Mar 2004 11:47:24 -0500 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) Content-Type: text/plain; charset="ISO-8859-1" X-OriginalArrivalTime: 02 Mar 2004 16:49:03.0546 (UTC) FILETIME=[44E8B9A0:01C40076] X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N The is a data structure, and its child tags ( and the like) are the data fields. I'm trying to get these data fields into the hashtable... here's the line-by-line explanation of what I require from the digester: // This happens before starting parse. // An instance of Contact is pushed into digester stack, // so that all operations will be performed on it. // This is done via line "digester.push(this);" // From there on, all XML lines are followed by an explanation // of the required operation // digester does nothing, obviously :) // digester does nothing, // since the appropriate Contact instance is already in stack 1 // digester executes set("RowId", "1") on Contact instance in stack Smith // digester executes set("FirstName", "Smith") on Contact instance in stack // digester does nothing -----Original Message----- From: Martin Kersten [mailto:Martin.Kersten@Student.Uni-Magdeburg.DE] Sent: Tuesday, March 02, 2004 11:34 AM To: Jakarta Commons Users List Subject: Re: [Digester] Parsing XML to a hashtable > I think I wasn't very clear about my problem - the Contact bean itself has a > hashmap, and "RowId" and "LastName" act as fields there. Here's the full > source just to eliminate all confusion: Well I am still confused :) What should happen when the xml gots parsed? Should the contact do what? I think you are thinking to complex and try to do two steps at once. What is put? Please can you use your example xml and explain on every tag, what digester should do at this certain point? You shouldn't be afraid to create helper objects. Martin (Kersten) > > public class Contact { > > // Actual bean contains 30+ fields, > // omitted for simplicity sake > private Map fields = new HashMap(); > > // Field name constants to use with get() > public static final String ROW_ID = "RowId"; > public static final String LAST_NAME = "LastName"; > > public String get(String name) { > return (String) fields.get(name); > } > > public void set(String name, String value) { > fields.put(name, value); > } > > public void populate(String xml) throws Exception { > > Digester d = new Digester(); > > // ********************************* > // Now what to put there for rules?? > // ********************************* > > digester.push(this); > > digester.parse(new StringReader(xml)); > > } > > public static void main(String[] args) throws Exception { > > String xml = "" + > "" + > "1" + > "1" + > ""; > > Contact c = new Contact(); > > c.populate(xml); > > // What I want to get is two entries in "fields" hashmap, > // ("RowId", "1") and > // ("LastName", "Smith") > } > > } > > Now, the SetProperty rule doesn't work there, because it operates on tag > attributes, not on the tag itself - again, unless I'm missing some obscure > parameter combination that make it work in a different way... > > Tried CallMethod rule, too, and CallParam seem to be unable to pick up the > tag name (e.g., "RowId") as a parameter value > > Any other ideas? > > Thanks, > > Alex > > -----Original Message----- > From: Martin Kersten [mailto:Martin.Kersten@Student.Uni-Magdeburg.DE] > Sent: Tuesday, March 02, 2004 9:19 AM > To: Jakarta Commons Users List > Subject: Re: [Digester] Parsing XML to a hashtable > > > Hi, > > > I have a HashMap-based java bean that I'm trying to populate from an XML > > file. Now, the only accessors java bean exposes are get(String) and > > set(String, String), and XML file contains its data in body text, like > that: > > > > > > 1 > > Smith > > > > > > Now, while the whole setup looks fairly common, it doesn't look like > there's > > an easy way to parse it... I tried the CallMethod rule, but apparently it > > can accept parameters from pretty much anywhere - from body text, tag > > attribute, even the tag node up the stack - except from the tag name > itself! > > Am I missing something there? > > > > Thanks in advance, > Check the addSetProperty method. Should help you, I guess. If not > compose the contact using a bean and add it to your map represented > by the next xml level tag (like ) using addSetNext(..). > > Example: > > addCreateObject("*/contact", Contact.class); > ... (initialize the contact rowId and lastName properties using > addSetProperties) > addSetNext("*/contact","addContact"); > > + top level (or next higher level). > addCreateObject("contacts", ContactMap.class); > > //add method > contacts.addContact(Contact contact) { > if(contact.isValid()) > contactMap.set(contact.getRowId(), contact.getLastName()); > } > > I think you can guess the meaning of it. > > Summary: Try addSetProperty rule first. If it is not working try the > second approach. > > > Bye, > > Martin (Kersten) > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org > For additional commands, e-mail: commons-user-help@jakarta.apache.org > > --------------------------------------------------------------------- > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org > For additional commands, e-mail: commons-user-help@jakarta.apache.org > --------------------------------------------------------------------- To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-user-help@jakarta.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-user-help@jakarta.apache.org