commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Pretterhofer, Guenter (ext.)" <Guenter.Pretterho...@Dresdner-Bank.com>
Subject [Digester] addSetProperties
Date Thu, 18 Mar 2004 18:32:58 GMT
hi all,
I suppose something went wrong with my attachments.
Cheers,
Günter

---------------------------------------------
Properties.xml:
---------------------------------------------
<property>
	<database name="db1">
		<user>user1</user>
	</database>
	<database name="db2">
		<user>user2</user>
	</database>
</property>
---------------------------------------------
import java.io.*;
import org.apache.commons.digester.Digester;

public class PropertiesParser {
	
	public static void main(String[] args) {
		PropertiesParser pp = new PropertiesParser();
		
		try {
			pp.run(args[0]);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void run(String XMLFile) {
		try {
			Digester digester = new Digester();
			digester.setValidating(false);

			digester.addObjectCreate("property",
Property.class);

			digester.addObjectCreate("property/database",
Database.class);
			
			digester.addSetNext("property/database",
"addDatabase");
			
			digester.addSetProperties("/property/database");
	
digester.addBeanPropertySetter("property/database/user", "user");

			System.out.println("XMLFile = " + XMLFile);
			File input = new File(XMLFile);
			Property p = (Property) digester.parse(input);

			// sample how to extract the values
			String user, name;
			Database d;

			System.out.println("\nfirst entry:");
			d = (Database) p.databases.get(0);

			user = d.getUser();
			System.out.println("user=" + user);

			name = d.getName();
			System.out.println("name=" + name);
			
			System.out.println("\nsecond entry:");
			d = (Database) p.databases.get(1);

			user = d.getUser();
			System.out.println("user=" + user);

			name = d.getName();
			System.out.println("name=" + name);

		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}
}

---------------------------------------------
import java.util.ArrayList;

public class Property {
	protected ArrayList databases;

	public Property() {
		databases = new ArrayList();
	}

	public void addDatabase(Database rhs) {
		databases.add(rhs);
	}

	public Database getDatabase(int index) {
		return (Database) databases.get(index);
	}
}

---------------------------------------------
public class Database {
	private String user;
	private String name;

	public Database() {
	}

	public Database(String user, String name) {
		this.user = user;
		this.name = name;
	}

	public String getUser() {
		return this.user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

---------------------------------------------

output:
---------------------------------------------
XMLFile = Properties.xml

first entry:
user=user1
name=null

second entry:
user=user2
name=null
---------------------------------------------


-----Ursprüngliche Nachricht-----
Von: Tatu Vanhanen [mailto:tatu.vanhanen@graftor.fi]
Gesendet am: Donnerstag, 18. März 2004 07:32
An: Jakarta Commons Users List
Betreff: RE: [Digester] addSetProperties

Check that the Database class has a valid bean setter method for the "name"
property, i.e.
"public void setName(String name)".

- Tatu V.

> -----Original Message-----
> From: Pretterhofer, Guenter (ext.)
> [mailto:Guenter.Pretterhofer@Dresdner-Bank.com]
> Sent: 17. maaliskuuta 2004 17:08
> To: commons-user@jakarta.apache.org
> Subject: [Digester] addSetProperties
>
>
> hi all,
>
> maybe somebody can help me with the following problem?
>
> xml-file:
> <property>
> 	<database name="db1">
> 		<user>user1</user>
> 	</database>
> 	<database name="db2">
> 		<user>user2</user>
> 	</database>
> </property>
>
> a small excerpt of the Java source:
> digester.addObjectCreate("property", Property.class);
> digester.addObjectCreate("property/database", Database.class);
> digester.addSetNext("property/database", "addDatabase");
> digester.addSetProperties("/property/database", "name", "name");
> digester.addBeanPropertySetter("property/database/user", "user");
>
>
> when i print out name and user the output is the following:
> user=user1
> name=null
> user=user2
> name=null
>
> I don't understand why name is always null ...
> what am I doing wrong?
>
> thanks for your help
> günter
>
>
> ---------------------------------------------------------------------
> 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


Mime
View raw message