I am parsing an XML file.
i.e.
<people>
<person firstname="John" lastname="Doe" age="50" />
<person firstname="Thomas" lastname="Jefferson" age="260" />
<people>
I have created Person.java with the following attributes:
private String firstName;
private String lastName;
private int age;
My main method parses the XML, loops through each person, and gets the attributes for each.
I need to create instances of my Person class.
I could write something like this:
Person person = new Person();
for (int i = 0; i < attributes.getLength; i++) {
if (attribute.getName(i) = "firstname") { person.firstName = attribute.getValue(i);}
if (attribute.getName(i) = "lastname") { person.lastName = attribute.getValue(i); }
if (attribute.getName(i) = "age") { person.age = attribute.getValue(i); }
}
Since my actual XML has quite a few attributes, I would rather do something like this:
Person person = new Person();
for (int i = 0; i < attributes.getLength(); i++) {
person[attribute.getName(i)] = attribute.getValue(i);
}
This doesn't work. Does anyone know if I can make this work?
Thanks,
Ray