遍历xml数据的Internet Explorer / MS Edge浏览器问题

时间:2016-10-10 10:45:05

标签: javascript html xml xml-parsing

我正试图遍历一段XML来为Web应用程序提取一些逻辑规则。我已经设法让我的JavaScript在Firefox,Chrome,Opera和Safari上运行,但我在MS浏览器中遇到控制台错误,声明“无法获得未定义或空引用的属性”长度'。

由于错误出现在代码的早期,我无法通过反复试验获得解决方案。

我的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 个答案:

答案 0 :(得分:0)

我发现通过将.children更改为.childNodes和getAttribute来使用jquery.attr,问题已解决