0

最近、Jquery を使用して JavaScript で小さな XML パーサーを作成する際に問題に遭遇しました。このタスクを達成するために使用しているコードは次のとおりです。

     /*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());
    }

返される子の数が 2 であるべきなのに、なぜ 3 なのかわかりません。Jquery のドキュメントに記載されているように、children 関数は XML ツリーの最初のレベルのみをチェックする必要があります。

テストとして使用される XML は次のとおりです。

<questionnaire>
  <questions>
      <question id="1">
          <number></number>
          <title>Q1</title>
          <answers >
              <answer type="TextInput">
                  <result>ss</result>
                  <questions>
          <title>Hidden</title>
         </questions>
              </answer>
          </answers>
      </question>
    <question id="2">
    <title>Q2</title>
    </question>
  </questions>
</questionnaire>

ご覧のとおり、質問の子の数は 2 です。

私が得た結果は次のとおりです。

XML : <question id="1">           <number/>           <title>Q1</title>           <answers>               <answer type="TextInput">                   <result>ss</result>                     <questions>                     <title>Hidden</title>                   </questions>                </answer>           </answers>          </question> question.js:24
XML : <question id="2">         <title>Q2</title>       </question> question.js:24
XML : <title>Hidden</title> question.js:24
Question Number: 3 

手伝って頂けますか?

4

1 に答える 1

0

そのようなプロパティがない場合、呼び出しthis.XMLObjectはエラーを引き起こしませんか?;var XMLObjectと同じではないものを定義しています。this.XMLObject

また、実際の変数がプロパティでないときに呼び出すとthis.$XMLObject、例外も発生するはずです。

別のこと:questionsXML.push(this)変数をインクリメントする代わりにを使用してみてくださいquestionNumber。そして、カウントを返すには、を使用しますreturn questionsXML.length

それとは別に、あなたのコードは正しい値を返すはずだと思います。または、もっとあるかもしれませんchildren(内部ノード/値)。

テストするXML:

<questionnaire>
    <questions>
        <question id="1">
            <number></number>
            <title>Q1</title>
            <answers>
                <answer type="TextInput">
                <result>ss</result>
            </answers>
        </question>
        <question id="2">
            <number></number>
            <title>Q1</title>
            <answers>
                <answer type="TextInput">
                <result>sSSs</result>
            </answers>
        </question>
    </questions>
</questionnaire>
于 2013-02-21T15:09:57.000 に答える