NSXML解析特定标签值

发布于 2024-10-22 06:59:21 字数 1894 浏览 4 评论 0原文

我有一个如下所示的 XML 文件:

 <country>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Larne</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Troon </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Larne</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Cairnryan </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Belfast</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Birkenhead </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Belfast</SourcePort>
                <DestinationCountry>Belgium</DestinationCountry>
                <DestinationPort>Heysham </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Belgium</SourceCountry>
                <SourcePort>Warrenpoint</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Heysham </DestinationPort>
                        </routes>

我想解析 的特定数据== 爱尔兰和==英国。我该怎么做?

I have an XML file that looks like this:

 <country>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Larne</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Troon </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Larne</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Cairnryan </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Belfast</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Birkenhead </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Ireland</SourceCountry>
                <SourcePort>Belfast</SourcePort>
                <DestinationCountry>Belgium</DestinationCountry>
                <DestinationPort>Heysham </DestinationPort>
                        </routes>
            <routes>
                <SourceCountry>Belgium</SourceCountry>
                <SourcePort>Warrenpoint</SourcePort>
                <DestinationCountry>UK</DestinationCountry>
                <DestinationPort>Heysham </DestinationPort>
                        </routes>

I want parse particular data for <SourceCountry> == Ireland and <DestinationCountry> == UK. How can I do this?

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

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

发布评论

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

评论(4

空城仅有旧梦在 2024-10-29 06:59:21

使用NSXMLParser

  1. 实现此类的委托...

  2. 实现此解析器的方法...

    您将需要以下方法..

    - (void)解析器:(NSXMLParser *)解析器 didStartElement:(NSString *)elementName 命名空间URI:(NSString *)namespaceURI 限定名称:(NSString *)限定名称属性:(NSDictionary *)attributeDict< /p>

    - (void)parser:(NSXMLParser *)parserfoundCharacters:(NSString *)string

您可以阅读 本教程;使用起来很简单。

Use NSXMLParser

  1. implement the delegate for this class ... <NSXMLParserDelegate>

  2. implement the methods for this parser...

    YOU will need the following methods..

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

You can read this tutorial; it is simple to use.

夜无邪 2024-10-29 06:59:21

您需要使用 XML 解析器,使用 NSXMLParser 来解析您的数据。
下面是苹果的 ImageMap 示例代码,其中他们使用 NSXMLParser 来解析 XML 内容。

http:// developer.apple.com/library/mac/#samplecode/ImageMap/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009015

You need to use XML parser, Use NSXMLParser to parse you data.
Below is the apple sample code of ImageMap in which they have used the NSXMLParser to parse XML content.

http://developer.apple.com/library/mac/#samplecode/ImageMap/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009015

給妳壹絲溫柔 2024-10-29 06:59:21

试试这个。 。 。

int element=0;
[self parseXMLFileAtURL:filepath];

以下方法是 NSXMLParser 的委托

- (void)parseXMLFileAtURL:(NSString *)URL
{   
    NSURL *xmlURL = [NSURL fileURLWithPath:URL];
    parser = [[ NSClassFromString(@"NSXMLParser") alloc] initWithContentsOfURL:xmlURL];
    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];

}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{   
    NSLog(@"found file and started parsing");
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{           
    if(([elementName compare:@"SourceCountry"])==NSOrderedSame)
        element=1;
    if(([elementName compare:@"SourcePort"])==NSOrderedSame)
        element=2;
    if(([elementName compare:@"DestinationCountry"])==NSOrderedSame)
        element=3;
    if(([elementName compare:@"DestinationPort"])==NSOrderedSame)
        element=4;


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


}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (element==1)
    {
        [TotalXMLArray addObject:string];       
        element=0;
    }
    if (element==2)
    {
        [TotalXMLArray addObject:string];       
        element=0;
    }
    if (element==3)
    {
        [TotalXMLArray addObject:string];       
        element=0;
    }
    if (element==4)
    {
        [TotalXMLArray addObject:string];       
        element=0;
    }

}

Try this . . .

int element=0;
[self parseXMLFileAtURL:filepath];

following methods are the delegates of NSXMLParser

- (void)parseXMLFileAtURL:(NSString *)URL
{   
    NSURL *xmlURL = [NSURL fileURLWithPath:URL];
    parser = [[ NSClassFromString(@"NSXMLParser") alloc] initWithContentsOfURL:xmlURL];
    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];

}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{   
    NSLog(@"found file and started parsing");
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{           
    if(([elementName compare:@"SourceCountry"])==NSOrderedSame)
        element=1;
    if(([elementName compare:@"SourcePort"])==NSOrderedSame)
        element=2;
    if(([elementName compare:@"DestinationCountry"])==NSOrderedSame)
        element=3;
    if(([elementName compare:@"DestinationPort"])==NSOrderedSame)
        element=4;


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


}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (element==1)
    {
        [TotalXMLArray addObject:string];       
        element=0;
    }
    if (element==2)
    {
        [TotalXMLArray addObject:string];       
        element=0;
    }
    if (element==3)
    {
        [TotalXMLArray addObject:string];       
        element=0;
    }
    if (element==4)
    {
        [TotalXMLArray addObject:string];       
        element=0;
    }

}

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