I have a problem with looping through XML structure. My XML Structure looks like:
<main>
<representation>
<representation>A</representation>
<class>B</class>
<notes/>
<room>C</room>
</representation>
<representation>
<representation>D</representation>
<class>E</class>
<notes>F</notes>
<room>G</room>
</representation>
</main>
. . .
EDIT:
What I want is to iterate trough every main node representation and pass the information into a table. The problem is I get the XML file with this structure and I can't influence it. So how can I only iterate trough every main node representation and skip the inner node (also called representation) ?
var columnContent1 = xmlDoc.getElementsByTagName("representation");
var tableContent = "";
for (i = 0 ; i<columnContent1.length; i++)
{
if (i % 2 == 1) {
tableContent += "<tr>";
tableContent += "<td>" + columnContent1[i].childNodes[0].childNodes[0].nodeValue + "</td>";
tableContent += "<td>" + columnContent1[i].childNodes[1].childNodes[0].nodeValue + "</td>";
tableContent += "<td>" + columnContent1[i].childNodes[2].childNodes[0].nodeValue + "</td>";
tableContent += "<td>" + columnContent1[i].childNodes[3].childNodes[0].nodeValue + "</td>";
tableContent += "</tr>";
}
};
tableBodyToday.innerHTML = tableContent;
In Chrome works okay, but not perfect.
in Firefox I get the error
TypeError: columnContent1[i].childNodes[0].childNodes[0] is undefined
How can I get the information like this?
<tr>
<td>A</td><td>B</td><td></td><td>C</td>
</tr>
<tr>
<td>D</td><td>E</td><td>F</td><td>G</td>
</tr>
If the node is the table data is empty. I think the solution is easy, but I don't get the correct solution.
if (i % 2 == 1) to skip every second innernode representation. Is there a better solution?
i%2==0also don't give the correct output..