R
Ramon F Herrera
I have a question for the XML crowd. I found a tutorial here:
http://www.totheriver.com/learn/xml/xmltutorial.html#6
which is very close to what I need, with one exception. The tutorial
traverses the XML file below:
<?xml version="1.0" encoding="UTF-8"?>
<Personnel>
<Employee type="permanent">
<Name>Seagull</Name>
<Id>3674</Id>
<Age>34</Age>
</Employee>
<Employee type="contract">
<Name>Robin</Name>
<Id>3675</Id>
<Age>25</Age>
</Employee>
<Employee type="permanent">
<Name>Crow</Name>
<Id>3676</Id>
<Age>28</Age>
</Employee>
</Personnel>
and display the fields. Pretty straightforward stuff. In the case
above the tag/field names are known in advance ("Name", "Id", and
"Age"). In my case, those names are not known until run time.
-Ramon
---------------------------------------------------------------
private Employee getEmployee(Element empEl) {
//for each <employee> element get text or int values of
//name ,id, age and name
// I DO NOT know these names below in advance.
// How can I retrieve them?. - Ramon
String name = getTextValue(empEl, "Name");
int id = getIntValue(empEl, "Id");
int age = getIntValue(empEl, "Age");
String type = empEl.getAttribute("type");
//Create a new Employee with the value read from the xml nodes
Employee e = new Employee(name, id, age, type);
return e;
}
private void parseDocument() {
//get the root elememt
Element docEle = dom.getDocumentElement();
//get a nodelist of <employee> elements
// I know the name of this top element. No problem here -
Ramon
NodeList nl = docEle.getElementsByTagName("Employee");
if (nl != null && nl.getLength() > 0) {
for (int i = 0; i < nl.getLength(); i++) {
//get the employee element
Element el = (Element) nl.item(i);
//get the Employee object
Employee e = getEmployee(el);
//add it to list
myEmpls.add(e);
}
}
}
http://www.totheriver.com/learn/xml/xmltutorial.html#6
which is very close to what I need, with one exception. The tutorial
traverses the XML file below:
<?xml version="1.0" encoding="UTF-8"?>
<Personnel>
<Employee type="permanent">
<Name>Seagull</Name>
<Id>3674</Id>
<Age>34</Age>
</Employee>
<Employee type="contract">
<Name>Robin</Name>
<Id>3675</Id>
<Age>25</Age>
</Employee>
<Employee type="permanent">
<Name>Crow</Name>
<Id>3676</Id>
<Age>28</Age>
</Employee>
</Personnel>
and display the fields. Pretty straightforward stuff. In the case
above the tag/field names are known in advance ("Name", "Id", and
"Age"). In my case, those names are not known until run time.
-Ramon
---------------------------------------------------------------
private Employee getEmployee(Element empEl) {
//for each <employee> element get text or int values of
//name ,id, age and name
// I DO NOT know these names below in advance.
// How can I retrieve them?. - Ramon
String name = getTextValue(empEl, "Name");
int id = getIntValue(empEl, "Id");
int age = getIntValue(empEl, "Age");
String type = empEl.getAttribute("type");
//Create a new Employee with the value read from the xml nodes
Employee e = new Employee(name, id, age, type);
return e;
}
private void parseDocument() {
//get the root elememt
Element docEle = dom.getDocumentElement();
//get a nodelist of <employee> elements
// I know the name of this top element. No problem here -
Ramon
NodeList nl = docEle.getElementsByTagName("Employee");
if (nl != null && nl.getLength() > 0) {
for (int i = 0; i < nl.getLength(); i++) {
//get the employee element
Element el = (Element) nl.item(i);
//get the Employee object
Employee e = getEmployee(el);
//add it to list
myEmpls.add(e);
}
}
}