【问题标题】:how to read HTML as XML in iphone如何在 iPhone 中将 HTML 读取为 XML
【发布时间】:2012-02-02 08:15:45
【问题描述】:
我有一个自定义的 wordpress 帖子,查看源代码时看起来像这样。
我怎样才能将它解析为 XML ?
<title>A sample quiz</title>
<content>Hello.</content>
<updatedDate>18/01/12</updatedDate>
<updatedTime>16:11</updatedTime>
<question>How many days are there in a week?</question>
<hint>No hint</hint>
<direction>Left</direction>
<answer1>4</answer1>
<answer2>5</answer2>
<answer3>6</answer3>
<answer4>7</answer4>
<correctAnswer>4</correctAnswer>
<score>20</score>
【问题讨论】:
标签:
iphone
ios
xcode
wordpress
【解决方案1】:
记得在你的h文件中设置。
NSXMLParserDelegate
并设置
- (void)didload
{
NSString *site = [NSString stringWithFormat:@"http://www.mysite.it/mydirectory/myxml"];
NSURL *xmlURL = [NSURL URLWithString:site];
myParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[myParser setDelegate:self];
[myParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"title"]) {
NSLog(@"element %@",attributeDict);
//do something
}
else if ([elementName isEqualToString:@"content"]){
NSLog(@"element %@",attributeDict);
//do something
}
//ecc
}
这是一种非常简单的方法。了解更多详情
http://www.xcode-tutorials.com/parsing-xml-files/