XML Newbie trying to traverse a DOM tree

Discussion in 'XML' started by Ramon F Herrera, Dec 15, 2007.

  1. 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);
    }
    }
    }
     
    Ramon F Herrera, Dec 15, 2007
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Dec 16, 2007
    #2
    1. Advertising

  3. On Dec 16, 8:30 am, Martin Honnen <> 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
     
    Ramon F Herrera, Dec 17, 2007
    #3
  4. Ramon F Herrera

    P. Lepin Guest

    Ramon F Herrera wrote:
    > On Dec 16, 8:30 am, Martin Honnen <> 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.
     
    P. Lepin, Dec 17, 2007
    #4
  5. 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/
     
    Martin Honnen, Dec 18, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joris Gillis
    Replies:
    2
    Views:
    1,543
    Joris Gillis
    Jun 16, 2006
  2. byron
    Replies:
    5
    Views:
    1,091
    Stefan Behnel
    May 30, 2009
  3. Martin C.
    Replies:
    8
    Views:
    383
    Josh Cheek
    Dec 6, 2010
  4. PerlFAQ Server

    FAQ 5.41 How do I traverse a directory tree?

    PerlFAQ Server, Jan 15, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    116
    PerlFAQ Server
    Jan 15, 2011
  5. PerlFAQ Server

    FAQ 5.41 How do I traverse a directory tree?

    PerlFAQ Server, Mar 31, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    289
    PerlFAQ Server
    Mar 31, 2011
Loading...

Share This Page