2017-07-28 74 views
1

我在工作的陣營原生應用和簡單試圖執行這段代碼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')

+0

我想你應該調用異步從http請求中獲取數據, 嘗試添加「等待獲取」並在你的函數前添加異步 –

+0

@GaneshCauda - 爲什麼?在適當的時候調用'.then'鏈 –

+0

我可以在原始XML中看到的唯一區別是它在**的作用**'<![CDATA ['和']]>'在它們自己的行上 - 而在失敗的數據中,您在一行中有'<![CDATA [...等等等等]]>' - 什麼是XMLParser?它是[這一個](https://www.npmjs.com/package/react-xml-parser)? –

回答

1

反應的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 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) { 

解決了這個問題太:對

+0

不幸的是,它沒有工作.... [你的代碼](https://ibb.co/mUoRaQ)和[輸出在這裏](https://ibb.co/cxWevQ) –

+0

嘗試更改'xmlParse .js'而不是...那個xmlParser扼殺空行! –

+0

其實,試試我剛剛更新的代碼的輕微變化 –

相關問題