I want to get the URL of image from this XML:
我想从这个XML中获取图像的URL:
<description>
<![CDATA[
<div><a href='http://www.ynet.co.il/articles/0,7340,L-4794118,00.html'><img src='http://images1.ynet.co.il/PicServer4/2016/04/20/6955445/small2.jpg' alt='null' title='null' border='0' width='100' height='56'></a></div>בפוליטיקה, בכלכלה, בביטחון, במשפט, בתרבות, באקדמיה. המתח העדתי עדיין איתנו בכל פינה. למה הגיעו כל כך מעט מזרחיים למוקדי הכוח בישראל? הנתונים המלאים, הניתוח והפרשנויות יתפרסמו ב-ynet בחג. צפו בהצצה
]]>
</description>
This is my code. I got empty array what's wrong with my code and what is the solution for my problem.
这是我的代码。我得到了空数组我的代码有什么问题,我的问题的解决方案是什么。
class ImageParser: NSObject, NSXMLParserDelegate{
var itemsStarted = false;
var descriptionStarted = false;
var divStarted = false;
var aStarted = false;
var inImage = false;
var images:[String] = [];
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
if(elementName == "item"){
itemsStarted = true;
}
if(itemsStarted && elementName == "description"){
descriptionStarted = true;
}
if(descriptionStarted && elementName == "div"){
divStarted = true;
}
if(divStarted && elementName == "a"){
inImage = true;
}
if(inImage && elementName == "img"){
images.append(attributeDict["src"]!);
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if(elementName == "img"){
inImage = false;
}
}
}
1 个解决方案
#1
2
NSXMLParser ignores anything within CDATA, so if your CDATA contains valid xml tags, what you need to do is basicly is run another instance of parser on the text inside CDATA.
NSXMLParser忽略CDATA中的任何内容,因此如果您的CDATA包含有效的xml标记,那么您需要做的就是在CDATA内的文本上运行另一个解析器实例。
this might help https://github.com/hunkier/XMLParser-Swift/blob/master/XMLParser.swift
这可能会有所帮助https://github.com/hunkier/XMLParser-Swift/blob/master/XMLParser.swift
also try adding this method to your code in swift 2:
也尝试在swift 2中将此方法添加到您的代码中:
func parser(parser: NSXMLParser!, foundCDATA CDATABlock: NSData!) {
var datastring = String(CDATABlock: NSData!, encoding: NSUTF8StringEncoding);
}
Use NSString
instead of String
in earlier version of swift...syntax is the same
在早期版本的swift中使用NSString而不是String ...语法是一样的
#1
2
NSXMLParser ignores anything within CDATA, so if your CDATA contains valid xml tags, what you need to do is basicly is run another instance of parser on the text inside CDATA.
NSXMLParser忽略CDATA中的任何内容,因此如果您的CDATA包含有效的xml标记,那么您需要做的就是在CDATA内的文本上运行另一个解析器实例。
this might help https://github.com/hunkier/XMLParser-Swift/blob/master/XMLParser.swift
这可能会有所帮助https://github.com/hunkier/XMLParser-Swift/blob/master/XMLParser.swift
also try adding this method to your code in swift 2:
也尝试在swift 2中将此方法添加到您的代码中:
func parser(parser: NSXMLParser!, foundCDATA CDATABlock: NSData!) {
var datastring = String(CDATABlock: NSData!, encoding: NSUTF8StringEncoding);
}
Use NSString
instead of String
in earlier version of swift...syntax is the same
在早期版本的swift中使用NSString而不是String ...语法是一样的