2016-10-10 4 views
2

ウェブアプリケーションのロジックルールを取得するためにXMLをトラバースしようとしています。私はJavaScriptをFirefox、Chrome、Opera、Safari上で動作させることができましたが、MSブラウザで「」というコンソールエラーが発生しました。プロパティの長さが未定義またはnullの参照 'を取得できません。Internet Explorer/MS EdgeブラウザのトラバースXMLデータの問題

コードの早い段階でエラーが表示されるため、試行錯誤を通じて解決策を得ることができません。

私のJavascriptのサンプル。

function conditionTrigger(id, callType) { 
    var parser = new DOMParser(), 
    xmlLogic = parser.parseFromString(configXML, "text/xml"), 
    tags = xmlLogic.getElementsByTagName('*'), 
    conditionsPassed = 0, 
    callType; 
    if (id.length >= 3) { 
     id = id.slice(3); 
    } 
    console.log(tags[0]); 
    console.log(tags[0].children.length); 
    console.log('ID to be checked against = ' + id); 
    console.log(tags.length + ' nodes.'); 
    console.log(tags[0].children.length + ' level 1 node children.'); 
    for(var i01 = 0; i01 < tags[0].children.length; i01++){ 
     console.log(tags[0].children[i01]); 
     if (tags[0].children[i01].nodeName == "Rules"){ 
     console.log(tags[0].children[i01].children.length + ' child nodes of ' +tags[0].children[i01].nodeName + '.'); 
     for(var i02 = 0; i02 < tags[0].children[i01].children.length; i02++){ 
      console.log(tags[0].children[i01].children[i02]); 
     } 
     } 
    } 
} 

私のxmlは以下です。

<?xml version="1.0" encoding="utf-8" ?> 
<output brand="[BRANDNAME]" title="[SCRIPTNAME]" version ="[SCRIPTVERSION]"> 
<Rules> 
<block id="1000000" triggerID="14"> 
    <ConditionsAndActions name="Test for something else" setIndex="2"> 
    <Conditions> 
    <Condition type="DataField" fieldID="6" value="xxx" /> 
    </Conditions> 
    <Actions> 
    <Action actionIndex="0" type="DisplayAlert" message="can't move on, xxx found"/> 
    </Actions> 
    </ConditionsAndActions> 
    <ConditionsAndActions name="Check if forename is 'test' and call is a callback" setIndex="1"> 
    <Conditions> 
    <Condition type="DataField" fieldID="6" value="test" /> 
    <Condition type="Callback" /> 
    </Conditions> 
    <Actions> 
    <Action actionIndex="0" type="MoveToPanel" panelID="27"/> 
    <Action actionIndex="0" type="DisplayAlert" message="Aha, this is a callback!"/> 
    </Actions> 
    </ConditionsAndActions> 
    <ConditionsAndActions name="Default" setIndex="0"> 
    <Conditions> 
    </Conditions> 
    <Actions> 
    <Action actionIndex="0" type="MoveToPanel" panelID="17"/> 
    </Actions> 
    </ConditionsAndActions> 
</block> 
<block id="1000001" triggerID="19"> 
    <ConditionsAndActions name="Default" setIndex="0"> 
    <Conditions> 
    </Conditions> 
    <Actions> 
    <Action actionIndex="0" type="MoveToPanel" panelID="18"/> 
    </Actions> 
    </ConditionsAndActions> 
</block> 
<block id="1000002" triggerID="20"> 
    <ConditionsAndActions name="Default" setIndex="0"> 
    <Conditions> 
    </Conditions> 
    <Actions> 
    <Action actionIndex="0" type="MoveToPanel" panelID="10"/> 
    </Actions> 
    </ConditionsAndActions> 
</block> 
<block id="1000003" triggerID="22"> 
    <ConditionsAndActions name="Default" setIndex="0"> 
    <Conditions> 
    </Conditions> 
    <Actions> 
    <Action actionIndex="0" type="MoveToPanel" panelID="10"/> 
    </Actions> 
    </ConditionsAndActions> 
</block> 
<block id="1000004" triggerID="23"> 
    <ConditionsAndActions name="Default" setIndex="0"> 
    <Conditions> 
    </Conditions> 
    <Actions> 
    <Action actionIndex="0" type="MoveToPanel" panelID="17"/> 
    </Actions> 
    </ConditionsAndActions> 
</block> 
</Rules> 
</output> 
+1

を解決したことがわかりましたか? 'getElementsByTagName( '*')で始まるのは、すべての要素を選択するときにデータにアクセスする奇妙な方法のようです。 'tags [0]'でアクセスしようとしているルート要素が必要な場合は、単に 'xmlLogic.documentElement'を使用します。それ以降、どの要素にアクセスしたいのか分かりませんが、 'querySelector(All)'と 'getElementsByTagName'を使って、必要な要素の型を指定することができます。 –

+1

また、StackoverflowではHTMLとJavascriptスニペットを実行可能なテスト可能な方法で挿入することができます。質問にスニペットを挿入し、問題を特定するためにIEまたはEdgeで確認およびテストできます。 –

+0

xmlがウェブアプリケーションの設定を保持しているので要素が変わります - 基本的にこの場合、ボタンをクリックするユーザーはIDを渡し、JSはxmlを走査してそのIDに関係する条件とアクションを見つけ出す必要があります。すべての条件が満たされていれば最高のsetIndex値を持つものを実行し、条件セットが満たされていなければdefsult(setIndex = 0)がトリガーされ、関連するactiobsが起動します –

答えて

0

私はこの問題をjquery.attr使用する.childNodesとのgetAttributeに.childrenを変更することで、あなたがあなたのXMLに探している要素を識別することができ