Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery incorrect children number XML parser
    text
    copied!<p>Recently I have encountered problems in writing a small XML parser in JavaScript using Jquery. The code which I am using for accomplish this task is as follows : </p> <pre><code> /*Private */ var XMLObject=$.parseXML(dataXml); var $XMLObject=$(XMLObject); var questionNumber=0; // array of XML objects var questionsXML= new Array(); /** Html questions */ //array of strings var questionsHTML= new Array(); /********************* * Constructor * *********************/ a=$XMLObject.find("questions"); a.children().each( function () { console.log( "XML : " + new XMLSerializer().serializeToString(this)); questionsXML[questionNumber]=this; questionNumber++; }); this.getXML=function () { var out = new XMLSerializer().serializeToString(this.XMLObject); return out; } this.getQuestionNumber=function() { return questionNumber; } // This function returns the question sequentially. this.getQuestion=function() { } Test function : function testXML () { var xml ="XML see later" var Obj= new XMLQuestions(xml); console.log("Question Number: " + Obj.getQuestionNumber()); } </code></pre> <p>I don't understand why the number of children returned is 3 when it should be 2. As it is stated in the Jquery documentation children function should only check the first level of the XML tree. </p> <p>The XML used as Test is the follwing one : </p> <pre><code>&lt;questionnaire&gt; &lt;questions&gt; &lt;question id="1"&gt; &lt;number&gt;&lt;/number&gt; &lt;title&gt;Q1&lt;/title&gt; &lt;answers &gt; &lt;answer type="TextInput"&gt; &lt;result&gt;ss&lt;/result&gt; &lt;questions&gt; &lt;title&gt;Hidden&lt;/title&gt; &lt;/questions&gt; &lt;/answer&gt; &lt;/answers&gt; &lt;/question&gt; &lt;question id="2"&gt; &lt;title&gt;Q2&lt;/title&gt; &lt;/question&gt; &lt;/questions&gt; &lt;/questionnaire&gt; </code></pre> <p>as you can see the number of question's children is two. </p> <p>The results I have got so fare are : </p> <pre><code>XML : &lt;question id="1"&gt; &lt;number/&gt; &lt;title&gt;Q1&lt;/title&gt; &lt;answers&gt; &lt;answer type="TextInput"&gt; &lt;result&gt;ss&lt;/result&gt; &lt;questions&gt; &lt;title&gt;Hidden&lt;/title&gt; &lt;/questions&gt; &lt;/answer&gt; &lt;/answers&gt; &lt;/question&gt; question.js:24 XML : &lt;question id="2"&gt; &lt;title&gt;Q2&lt;/title&gt; &lt;/question&gt; question.js:24 XML : &lt;title&gt;Hidden&lt;/title&gt; question.js:24 Question Number: 3 </code></pre> <p>Can you help me? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload