我在 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 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) {
问题也解决了:p
关于javascript - XMLParser 给出错误 '' 无法设置未定义的属性值'',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45365663/