I have this kind of dynamic XML to parse, where a section tag always has an "allSubSections" tag, which may or may not be filled with more sections (of the exact same xml format). Since each section has the "allSubSections" tag, it could theoretically go very very deep with nested sections, but in real life usage, usually goes only a few levels deep. There is only ever one "allSections" tag in my XML document. Here is the XML:
<poAssessMentVersion>
<allSections>
<section>
<allSubSections>
<section>
<allSubSections></allSubSections>
</section>
<section>
<allSubSections>
<section>
<allSubSections></allSubSections>
<section>
</allSubSections>
</section>
</allSubSections>
<section>
<allSections>
</poAssessmentVersion>
So I need to cycle through, creating javascript objects out of this xml. As with the XML, an object may have subsections of a similar type to itself. I believe I need to cycle through only the direct children of "allSections" with the tag name of "section" as the first step. How would I do that? I just asked a question Here, about doing that, but not cycling through a collection tag and got a great answer. Now I need the equivilent answer for this collection tag "allSections", Here is my current code:
var theSections = xml.querySelectorAll("poAssessmentVersion>allSections");
for (i = 0; i < theSections.length; i++) {
var section = new AssessmentSection();
section.description = theSections[i].querySelectorAll("AssessmentSection>description")[theSections.length - i].childNodes[0].nodeValue;
version.allSections.push(section);
}
Is there some way to cycle through the tag getting only the immeditate children ? I feel like my current code is getting close but The order of my list of sections is not quite right. In my example XML I wrote <AssessmentSection> as <section> but it is really <assessmentSection> in my xml.
No comments:
Post a Comment