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

标签 javascript html xml xml-parsing

我正在尝试遍历一段 XML 以提取网络应用程序的一些逻辑规则。我已设法让我的 JavaScript 在 Firefox、Chrome、Opera 和 Safari 上运行,但在 MS 浏览器中出现控制台错误,显示“无法获取未定义或空引用的属性‘长度’”。。 p>

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

我的 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> 

最佳答案

我发现通过将 .children 更改为 .childNodes 并将 getAttribute 更改为使用 jquery.attr,问题得到解决

关于javascript - Internet Explorer/MS Edge 浏览器问题遍历 xml 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39956490/

相关文章:

javascript - 首次单击某个按钮时到处都是 Div

javascript - 一页中的多个表单-reactjs

c# - namespace 不是其父 namespace 的 XML 属性被反序列化为 null

xml - 使用 xslt 更改属性值

javascript - 如何在我们的[跨域问题]之外的另一个域上加载XML文件

javascript - Onsubmit window.location 不工作

javascript - 将 momentjs 与重复日期一起使用

javascript - 我在这个 jQuery 链接中做错了什么?

html - 强制图像重新加载html5

PHP:如何在 "for each"循环之前按属性按字母顺序对 XML 进行排序