2016-10-10 121 views
2

我正在嘗試遍歷一段XML來爲Web應用程序提取一些邏輯規則。我設法讓我的JavaScript可以在Firefox,Chrome,Opera和Safari上運行,但是我在MS瀏覽器中收到了控制檯錯誤,指出'無法獲取未定義的屬性'length'或空引用''。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

你能確定你的XML,你正在尋找的元素呢?從'getElementsByTagName('*')開始''看起來像是一種奇怪的訪問數據的方式,因爲它選擇所有元素。如果你想使用'tags [0]'來訪問根元素,我只需使用'xmlLogic.documentElement'。從那時起,我不確定你想訪問哪些元素,但我確定'querySelector(All)'和'getElementsByTagName'允許你指定你想要的元素的類型。 –

+1

此外,Stackoverflow允許您以可執行的方式插入HTML和Javascript代碼片段,可測試的方式,考慮在您的問題中插入片段,我們可以在IE或Edge中檢查並測試以確定問題。 –

+0

這些元素會隨着xml爲Web應用程序攜帶配置而變化 - 基本上,在這種情況下,用戶單擊按鈕將傳入一個ID並且JS需要遍歷xml以找出與該id相關的條件和操作,如果滿足所有條件,則執行setIndex值最高的那個值,如果未滿足條件集,則會觸發defsult(setIndex = 0)並且其相關的actiobs將會觸發 –

回答

0

我發現,通過改變。孩子到.childNodes和的getAttribute使用jquery.attr的問題就解決了