javascript - XMLParser 给出错误 '' 无法设置未定义的属性值''

标签 javascript xml react-native xml-parsing

我在 React Native App 中工作,只是想执行这段代码

fetch('https://ad13.adfarm1.adition.com/banner?sid=3915124&wpt=X.xml')
.then((response) => response.text())
.then((responseJson) => {
    console.log("WorkingTillHere",responseJson)
    xml_Img = new XMLParser().parseFromString(responseJson);    // Assume xmlText contains the example XML
    console.log("ParserVideo,",xml_Img);
}) .catch((error) => {
    console.log("ParserEx",error);
});

我可以在控制台窗口中看到

WorkingTillHere

但它不执行 XMLParser().parseFromString(responseJson); 并且正在获取控制台日志

ParserEx TypeError: Cannot set property 'value' of undefined

相同的代码与此 URL 链接 fetch('http://teststream.airtango.de/theo/vast.xml')

完美配合

最佳答案

react-xml-parser 不理解

<?xml version="1.0" encoding="UTF-8"?>

标题。所以,一般来说,你可以

fetch('https://ad13.adfarm1.adition.com/banner?sid=3915124&wpt=X.xml')
.then((response) => response.text())
.then((xmlText) => {
    // remove <?xml  ... etc header because react-xml-parser chokes on it
    if (xmlText.toLowerCase().substr(0,5) == '<?xml') {
        xmlText = xmlText.split(/\?>\r{0,1}\n{0,1}/).slice(1).join('?>\n');
    }
    console.log("WorkingTillHere",xmlText)
    xml_Img = new XMLParser().parseFromString(xmlText);    // Assume xmlText contains the example XML
    console.log("ParserVideo,",xml_Img);
}) .catch((error) => {
    console.log("ParserEx",error);
});

The above would only catch it if the very first 5 characters are <?xml ... that may be a little naive on my part. However, I believe the authors of react-xml-parser should handle <?xml ... ?> in their code :p

Looking at the source to xmlParser, it seems they DO try to handle it, but obviously fail

注意,将 xmlParse.js 的第 8 行从

if (tags[i].indexOf('?xml')) {

if (tags[i].indexOf('?xml') < 0 && tags[i].length > 0) {

问题也解决了:p

关于javascript - XMLParser 给出错误 '' 无法设置未定义的属性值'',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45365663/

相关文章:

javascript - npx react-native 链接命令在最新版本的 react native 中不起作用。我们如何在我的项目中链接自定义字体系列

ios - react native iOS : Could not build module 'yoga' : 'algorithm' file not found

javascript - 通用Ajax错误处理-Angular JS中的拦截器

javascript - Jest XMLHttpRequest 模拟请求显示错误

javascript - 如何隐藏未选择的选项

xml - 元素 "context"的前缀 "context:component-scan"未绑定(bind)

javascript - 如何从 Internet Explorer DOM 调用 javascript

php - 需要使用 MySQL 查询插入封装的 XML 元素

java - Android 在点击发送按钮后停止工作

android - 设置状态栏时 View 隐藏