0

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?

5
  • What does not work about it? You never stated the problem. Commented Oct 21, 2013 at 13:39
  • Just noticed something, columnContent1 & columnContent is there some other code you have not included? Commented Oct 21, 2013 at 13:42
  • Have you tried with i%2==0 ? Commented Oct 21, 2013 at 13:42
  • the problem is that childNotes[0] works, but 1,2,3 don't work.. Commented Oct 21, 2013 at 14:07
  • i%2==0 also don't give the correct output.. Commented Oct 21, 2013 at 14:09

1 Answer 1

1

I see 3 issues, I have made a fiddle for you here http://jsfiddle.net/MgQf8/1/

I am not 100% sure what you want as the output but firstly..

When you call:

document.getElementsByTagName("representation");

You are selecting parent node and child node, so in your case your list is 4 long, and only 2 of the nodes actually have children (maybe that is why you are doing this if (i %2)? So I changed it, normally in XML you would use a root node of some kind and then iterate at every child from that position (assuming each child will be named representation therefore ignoring any sub child representation nodes) which I think is causing you some confusion maybe?

Thus you could use:

var columnContent1 = document.getElementsByTagName("root");
for (i = 0 ; i<columnContent1.children.length; i++)

Thirdly the way in which you are trying to get back the values from each child will not work since there is no value for you there, if you use console.dir in chrome you will be able to see the objects structure from there.

columnContent1[i].children[0].innerText
// Returns value `A` of a representationChild node.

I hope this helps shed some light.

Sign up to request clarification or add additional context in comments.

2 Comments

First, thanks.. what i want is to iterate trough every main node representation. 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) ?
by default all XML should go from a root node or '/' in this case. You could try from the XmlDoc root level run a for loop on its children such as 'for (i = 0 ; i<xmlDoc.children.length; i++)' which will iterate through each 'representation' node ignoring the inner children. You are essentially doing a for loop in a for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.