XMLParser中给错误'无法设置的未定义的属性值'
我在工作的阵营原生应用和简单试图执行这段代码XMLParser中给错误'无法设置的未定义的属性值'
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
相同的代码工作p erfectly好吗这个URL链接fetch('http://teststream.airtango.de/theo/vast.xml')
反应的XML解析器并不了解
<?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 ofreact-xml-parser
should handle<?xml ... ?>
in their code :pLooking 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) {
解决了这个问题太:对
不幸的是,它没有工作.... [你的代码](https://ibb.co/mUoRaQ)和[输出在这里](https://ibb.co/cxWevQ) –
尝试更改'xmlParse .js'而不是...那个xmlParser扼杀空行! –
其实,试试我刚刚更新的代码的轻微变化 –
我想你应该调用异步从http请求中获取数据, 尝试添加“等待获取”并在你的函数前添加异步 –
@GaneshCauda - 为什么?在适当的时候调用'.then'链 –
我可以在原始XML中看到的唯一区别是它在**的作用**''在它们自己的行上 - 而在失败的数据中,您在一行中有' ' - 什么是XMLParser?它是[这一个](https://www.npmjs.com/package/react-xml-parser)? –