从Cocoa中解析一个简单的XML字符串? [英] Parsing a simple XML from string in Cocoa?

查看:127
本文介绍了从Cocoa中解析一个简单的XML字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的XML,我需要从puid-list中获取第一 id 。我发现了很多例子,但没有一个是因为命名空间。



PS:我如何将 ID m。Mac

 < genpuid songs =1xmlns:mip =http://musicip.com/ ns / mip-1.0#> 
< track file =/ htdocs / test.mp3puid =0c9f2f0e-e72a-c461-9b9a-e18e8964ca20>
< puid-list>
< puid id =0c9f2f0e-e72a-c461-9b9a-e18e8964ca20/>
< / puid-list>
< / track>
< / genpuid>


解决方案

您应该使用 NSXMLParser 。在代码中创建一个实例,并让它解析:

  NSData * XMLData = [myXMLString dataUsingEncoding:NSUnicodeStringEncoding]; 
NSXMLParser * parser = [[NSXMLParser alloc] initWithData:XMLData];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:YES]; //如果你需要
[parser parse]; //开始解析

那么你需要实现 NSXMLParserDelegate 在解析期间获取回调:

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

/ /

if([elementName isEqualToString:@puid]){
NSString * ID = [attributeDict objectForKey:@id];
//使用ID字符串,或将其存储在一个ivar中,以便您可以访问它在其他地方
}
}

处理命名空间的注意事项:



如果设置命名空间, elementName 是限定名,所以可以有前缀(例如 mip:puid )如果你在Mac上, NSXMLNode 有方便类的方法, localNameForName: prefixForName:得到字符串的两个部分。 p>

此外,您可能需要 NXXMLParser 委托方法 parser:didStartMappingPrefix:toURI: parser:didEndMappingPrefix:



注意 NSXMLParser 是那些从字符串(关于它们是否带有前缀)。因此,属性很少被命名为 mip:id ,但如果你想要健壮,你还需要检查这个。


I have a simple XML and I need to get the first 'id' from puid-list. I found lots of examples but none of them quite do that because of the namespace. How do get the id out as an NSString?

PS: I'm on a Mac.

<genpuid songs="1" xmlns:mip="http://musicip.com/ns/mip-1.0#">
  <track file="/htdocs/test.mp3" puid="0c9f2f0e-e72a-c461-9b9a-e18e8964ca20">
    <puid-list>
      <puid id="0c9f2f0e-e72a-c461-9b9a-e18e8964ca20"/>
    </puid-list>
  </track>
</genpuid>

解决方案

You should use NSXMLParser. Create an instance in your code and tell it to parse:

NSData * XMLData = [myXMLString dataUsingEncoding:NSUnicodeStringEncoding];
NSXMLParser * parser = [[NSXMLParser alloc] initWithData:XMLData];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:YES]; // if you need to
[parser parse]; // start parsing

then you need to implement the methods of NSXMLParserDelegate to get callbacks during parsing:

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

    /* handle namespaces here if you want */

    if([elementName isEqualToString:@"puid"]){
         NSString * ID = [attributeDict objectForKey:@"id"];
         // use ID string, or store it in an ivar so you can access it somewhere else
    }
}

Notes on handling namespaces:

If the namespaces are set, elementName is the qualified name, so could have prefix (e.g. mip:puid) If you're on the Mac, NSXMLNode has to convenience class methods, localNameForName: and prefixForName: to get the two parts of the string.

Also, You might want the NXXMLParser delegate methods parser:didStartMappingPrefix:toURI: and parser:didEndMappingPrefix:.

Note that names returned by NSXMLParser are exactly those from the string (regarding whether they're prefixed). So it's rare for the attribute to be named mip:id, but if you want to be robust, you need to check for this as well.

这篇关于从Cocoa中解析一个简单的XML字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆