0 Replies Latest reply on Jul 15, 2013 6:15 PM by Javaman

    XML - reading in a sequence

    Javaman

      Hello all,

       

      I am consuming an XML file using the Java API for XML (JAXP), and I have a sequence of Nodes that will be inside a 'container' node. There will only ever be exactly one container node. Here is a simplistic example:

       

      {code}

      <Employees>

           <Employee>

                <Name>Jim Bob</Name>

                <Title>Programmer</Title>

           </Employee>

           <Employee>

                <Name>Pepe Roni</Name>

                <Title>Analyst</Title>

           </Employee>

           <Employee>

                <Name>Lynn Gweeny</Name>

                <Title>Vice President</Title>

           </Employee>

      </Employees>

      {/code}

       

      In this example, the <Employees> tag is nothing more than a container for the list of <Employee> nodes. There will always be exactly one <Employees> node. Right now I am reading this in as follows:

       

      {code}

      Node employees = doc.getElementsByTagName("Employees").item(0);

      Element employeeElement = (Element) employees;

      NodeList empList = employeeElement.getElementsByTagName("Employee");

      {/code}

       

      This works, but I have a suspicion that this is not the best way to get the list of employee nodes. Certainly not the most elegant. Is there a better way to do this?

      Thanks,

      -Jim