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

时间:2017-07-28 05:41:53

标签: 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:无法设置undefined

的属性'value'

使用此网址链接fetch('http://teststream.airtango.de/theo/vast.xml')

,相同的代码完美无缺

1 个答案:

答案 0 :(得分:1)

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);
});
  

如果前5个字符是<?xml,那么上面只会抓住它......这对我来说可能有点天真。但是,我相信react-xml-parser的作者应在其代码中处理<?xml ... ?>:p

     

查看xmlParser的来源,似乎他们试图处理它,但显然失败了

注意,从

更改xmlParse.js的第8行
if (tags[i].indexOf('?xml')) {

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

也解决了这个问题:p