iPhone如何使用NSXMLParser获取CDATA属性?
我想从在线 XML 进行解析,其中 XML 如下所示:
<menu maincategory="![CDATA[Erhverv]]" subcategory="![CDATA[Erhvervsregister]]" category="![CDATA[Akupunktur]]"/>
在函数解析器 didStartElement 中,我使用如下所示:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"menu"]) {
NSString *maincategory = [attributeDict objectForKey:@"maincategory"];
}
}
使用 %@ 输出如下所示:
attributeDict:{
category = "![CDATA[Advokater]]";
maincategory = "![CDATA[Erhverv]]";
subcategory = "![CDATA[Erhvervsregister]]";
}
2011-05-27 19:20:42.663 XML[62494:840b] maincategory using %s: `ãâå
2011-05-27 19:20:42.663 XML[62494:840b] maincategory using %@: ![CDATA[Erhverv]]
但我需要不带 ![CDATA] 标签的输出
任何人都可以帮忙我该如何解决这个问题? 提前致谢
I want to parse from an online XML where the XML is like below:
<menu maincategory="![CDATA[Erhverv]]" subcategory="![CDATA[Erhvervsregister]]" category="![CDATA[Akupunktur]]"/>
In the function parser didStartElement i used like below:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"menu"]) {
NSString *maincategory = [attributeDict objectForKey:@"maincategory"];
}
}
The output is like below using %@:
attributeDict:{
category = "![CDATA[Advokater]]";
maincategory = "![CDATA[Erhverv]]";
subcategory = "![CDATA[Erhvervsregister]]";
}
2011-05-27 19:20:42.663 XML[62494:840b] maincategory using %s: `ãâå
2011-05-27 19:20:42.663 XML[62494:840b] maincategory using %@: ![CDATA[Erhverv]]
But i need the output without the ![CDATA] tag
Can anyone please help me that how can i solve that?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将使用 NSRegularExpression 对象对 attributeDict 进行后期处理。对于字典中的每个字符串,将其复制到 NSMutableString(在下面的代码中名为 theString)并运行以下代码:
I would post process the attributeDict with an
NSRegularExpression
object. For each string in the dictionary, copy it into an NSMutableString (named theString in my code below) and run this code: