iPhone 上的 XML 解析

发布于 2024-10-06 11:11:33 字数 1281 浏览 4 评论 0原文

我想为 iPhone 做一些简单的 XML 解析。主要是为了练习。我认为 XML 非常简单,我想学习如何将它与其他语言集成。

我使用此链接来获取有关如何进行 XML 解析的教程,但对于我的目的而言,它有点先进:/

是否有一种简单的方法不需要很多行代码来进行 XML 解析?

http://webcache.googleusercontent.com/search?q=cache:4S0UStrJP28J:gigaom.com/apple/tutorial- build-a-simple-rss-reader-for-iphone/+xml+parser+iphone&cd=3&hl=en&ct=clnk&client=safari

致以最诚挚的问候!

克里斯蒂安

编辑:

我能够通过此实现我想要的:

- (void)startParsing {
    NSData *xmlData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.vegvesen.no/trafikk/xml/savedsearch.xml?id=604"]];
    NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:xmlData] autorelease];
    [parser setDelegate:self];
    [parser parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    NSLog(@"Started %@", elementName);
}

这只会打印出 elementName...我应该如何从这里继续获取每个元素的值?

I wanna do some simple XML parsing for the iPhone. Mostly for practice. I think that XML is really easy and i wanna learn how to integrate it with other languages.

i used this link for a tutorial on how do XML parsing, but it was a little bit to advanced for my purpose :/

isn't there a simple way that does not require many lines of code to do XML parsing?

http://webcache.googleusercontent.com/search?q=cache:4S0UStrJP28J:gigaom.com/apple/tutorial-build-a-simple-rss-reader-for-iphone/+xml+parser+iphone&cd=3&hl=en&ct=clnk&client=safari

Best regards!

Kristian

EDIT:

I was able to achieve what i wanted with this:

- (void)startParsing {
    NSData *xmlData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.vegvesen.no/trafikk/xml/savedsearch.xml?id=604"]];
    NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:xmlData] autorelease];
    [parser setDelegate:self];
    [parser parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    NSLog(@"Started %@", elementName);
}

this will just print out the elementName... how should i go on from here to get the value from each element?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画离情绘悲伤 2024-10-13 11:11:33

虽然知道这一点很好,但实际上,使用 JSON 可能会更轻松

While it's good to know, in practice you might have a much easier time with JSON

暗藏城府 2024-10-13 11:11:33

我想你一定已经制作了这样的模型类:

import

CarDetails.h

@interface CarDetails : NSObject
{
    NSString *MapName;
    NSString *MapContentName;
    NSString *MapWidth;
    NSString *MapHeight;
}

@property(retain,nonatomic)NSString *MapName;
@property(retain,nonatomic)NSString *MapContentName;
@property(retain,nonatomic)NSString *MapWidth;
@property(retain,nonatomic)NSString *MapHeight;

CarDetails.m

import "CarDetails.h"

@implementation CarDetails

@synthesize MapName;
@synthesize MapContentName;
@synthesize地图宽度,地图高度;

@end

看到这些是你全局存储值的模型对象,你可以使用这里的值

现在我们使用三个xml解析器委托

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
   if([elementName isEqualToString:@"car"]){
    carDetails =[[carDetails alloc]init];
}

}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
{

if([elementName isEqualToString:@"title"]){
    car.mapName=nodecontent;
}
else if([elementName isEqualToString:@"link"]){
    car.MapContenName=nodecontent;
}

//finally when we reaches the end of tag i am adding data inside the NSMutableArray
if([elementName isEqualToString:@"item"]){

    [rssOutputData addObject:car]; // adding the model class object to the array and   then use this array to retrieve the values
    [car release];
    car = nil;
}
//release the data from mutable string variable
[nodecontent release];

//reallocate the memory to get new content data from file
nodecontent=[[NSMutableString alloc]init];

}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
   currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

1)在didStart中,只有当你找到第一个节点时,你才会分配模型类对象
2)在foundCharacters中添加数据
3)在didEndElement中,您将把foundCharacters中的数据分配给模型对象

i suppose you must have made a model class like this :

import

CarDetails.h

@interface CarDetails : NSObject
{
    NSString *MapName;
    NSString *MapContentName;
    NSString *MapWidth;
    NSString *MapHeight;
}

@property(retain,nonatomic)NSString *MapName;
@property(retain,nonatomic)NSString *MapContentName;
@property(retain,nonatomic)NSString *MapWidth;
@property(retain,nonatomic)NSString *MapHeight;

CarDetails.m

import "CarDetails.h"

@implementation CarDetails

@synthesize MapName;
@synthesize MapContentName;
@synthesize MapWidth,MapHeight;

@end

See these are the model objects where u store ur values globally and you can use the values from here

now we use three xml parser delegates

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
   if([elementName isEqualToString:@"car"]){
    carDetails =[[carDetails alloc]init];
}

}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
{

if([elementName isEqualToString:@"title"]){
    car.mapName=nodecontent;
}
else if([elementName isEqualToString:@"link"]){
    car.MapContenName=nodecontent;
}

//finally when we reaches the end of tag i am adding data inside the NSMutableArray
if([elementName isEqualToString:@"item"]){

    [rssOutputData addObject:car]; // adding the model class object to the array and   then use this array to retrieve the values
    [car release];
    car = nil;
}
//release the data from mutable string variable
[nodecontent release];

//reallocate the memory to get new content data from file
nodecontent=[[NSMutableString alloc]init];

}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
   currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

1) in didStart you will allocate the model class object only if you find the very first node
2) in foundCharacters you append the data
3) in didEndElement you will assigh the data from foundCharacters to the model object

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文