XML Newbie trying to traverse a DOM tree

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);
}
}
}
 
M

Martin Honnen

Ramon said:
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.

You can either loop through the child nodes or call
getElementsByTagName("*") and then access the node name.
 
R

Ramon F Herrera

You can either loop through the child nodes or call
> getElementsByTagName("*") and then access the node name.


Hmm, that is very interesting. Is that asterisk part of a full regular
expression matcher?

Thanks!

-Ramon
 
P

P. Lepin

Ramon said:
Hmm, that is very interesting. Is that asterisk part of a full regular
expression matcher?

No. See DOM Level 3 Core, Document and Element interfaces.

If you want a powerful XML addressing language, you should look into XPath.
 
M

Martin Honnen

Ramon said:
Hmm, that is very interesting. Is that asterisk part of a full regular
expression matcher?

No. It is the only and sole form of a wildcard that getElementsByTagName
supports, it matches all elements.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top