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>
你能确定你的XML,你正在寻找的元素呢?从'getElementsByTagName('*')开始''看起来像是一种奇怪的访问数据的方式,因为它选择所有元素。如果你想使用'tags [0]'来访问根元素,我只需使用'xmlLogic.documentElement'。从那时起,我不确定你想访问哪些元素,但我确定'querySelector(All)'和'getElementsByTagName'允许你指定你想要的元素的类型。 –
此外,Stackoverflow允许您以可执行的方式插入HTML和Javascript代码片段,可测试的方式,考虑在您的问题中插入片段,我们可以在IE或Edge中检查并测试以确定问题。 –
这些元素会随着xml为Web应用程序携带配置而变化 - 基本上,在这种情况下,用户单击按钮将传入一个ID并且JS需要遍历xml以找出与该id相关的条件和操作,如果满足所有条件,则执行setIndex值最高的那个值,如果未满足条件集,则会触发defsult(setIndex = 0)并且其相关的actiobs将会触发 –