Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XML Newbie trying to traverse a DOM tree

Reply
Thread Tools

XML Newbie trying to traverse a DOM tree

 
 
Ramon F Herrera
Guest
Posts: n/a
 
      12-15-2007
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);
}
}
}

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      12-16-2007
Ramon F Herrera wrote:

> 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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Ramon F Herrera
Guest
Posts: n/a
 
      12-17-2007
On Dec 16, 8:30 am, Martin Honnen <(E-Mail Removed)> wrote:
> Ramon F Herrera wrote:
> > 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.
>



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

Thanks!

-Ramon

 
Reply With Quote
 
P. Lepin
Guest
Posts: n/a
 
      12-17-2007

Ramon F Herrera wrote:
> On Dec 16, 8:30 am, Martin Honnen <(E-Mail Removed)> wrote:
>> Ramon F Herrera wrote:
>> > 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.

>
> 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.

--
Presented in Brain Control where available.
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      12-18-2007
Ramon F Herrera wrote:

>> 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?


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


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.41 How do I traverse a directory tree? PerlFAQ Server Perl Misc 0 03-31-2011 04:00 PM
FAQ 5.41 How do I traverse a directory tree? PerlFAQ Server Perl Misc 0 01-15-2011 05:00 PM
Traverse YAML node tree with non-unique values Martin C. Ruby 8 12-06-2010 12:20 PM
lxml: traverse xml tree and retrieve element based on an attribute byron Python 5 05-30-2009 08:00 AM
insert E4X XML tree inside existing DOM tree Joris Gillis XML 2 06-16-2006 08:30 PM



Advertisments