在Cocoa中解析NSXMLElement [英] Parsing an NSXMLElement in Cocoa

查看:184
本文介绍了在Cocoa中解析NSXMLElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用XPath来解析XML文档。我的一个NSXMLElement看起来像下面的假设:

 < foo bar =yummy 



我试图获取属性栏的值,但是我使用的代码的任何解释,给我回bar =woo,这意味着我需要做进一步的字符串处理,以获得单独的woo和woo。



基本上我在做某事like

  NSArray * nodes = [xmlDoc nodesForXPath:@foo / @ barerror:& 
xmlElement = [nodes objectAtIndex:0];

有没有写上面的代码只是给我美味,而不是bar =yummy





$ b b p>假设TouchXML正在被使用,是否仍然有获得类似的结果?在抓取属性的值,没有属性=值?

解决方案

TouchXML API应该是一个精确的重复的Apple的NSXML实现,所以它应该是相同的,除非你将替换所有NS方法的C方法。


TouxhXML类直接映射到NSXML类。 NSXMLNode - > CXMLNode NSXMLDocument - > CXMLDocument NSXMLElement - > CXMLElement 。由于显而易见的原因,您不能混合和匹配NSXML和TouchXML类。



TouxhXML方法也直接映射到NSXML方法,但只支持一小部分方法。常量和枚举名称几乎是等价的(CXML ... VS NSXML ...),但常量值不等于


参考



使用普通的Apple类或者TouchXML框架下面的代码将给你刚才的字符串yummy为你的例子。

  NSArray * nodes = [xmlDoc nodesForXPath:@./ foo / @ barerror:& err]; 
NSString * value = [[nodes objectAtIndex:0] stringValue];

好运,



Brian G


I'm trying to use XPath to parse an XML document. One of my NSXMLElement's looks like the following, hypothetically speaking:

<foo bar="yummy">

I'm trying to get the value for the attribute bar, however any interpretation of code I use, gives me back bar="woo", which means I need to do further string processing in order to obtain access to woo and woo alone.

Essentially I'm doing something like

NSArray *nodes = [xmlDoc nodesForXPath:@"foo/@bar" error:&error];
xmlElement = [nodes objectAtIndex:0];

Is there anyway to write the code above to just give me yummy, versus bar="yummy" so I can relieve myself of parsing the string?

Thanks.


Assuming TouchXML is being used, is there still anyway to obtain similar results? As in grabbing just the value for the attribute, without the attribute="value"? That results in then further having to parse the string to get the value out.

解决方案

The TouchXML API is supposed to be an exact duplicate of Apple's NSXML implementation, so it should be the same except you'll replaces all NS-Method's with C-Methods.

The TouxhXML classes map directly to the NSXML classes. NSXMLNode -> CXMLNode, NSXMLDocument -> CXMLDocument, NSXMLElement -> CXMLElement. For obvious reasons you can't mix and match NSXML and TouchXML classes.

The TouxhXML methods map directly to NSXML methods as well, but only a small subset of methods are supported. Constant and enum names are almost equivalent (CXML... vs NSXML...) but constant values will not be equival

reference

Using either the regular Apple classes or the TouchXML framework the following code will give you just the string "yummy" for your example.

 NSArray *nodes = [xmlDoc nodesForXPath:@"./foo/@bar" error:&err];
 NSString *value = [[nodes objectAtIndex:0] stringValue];

Good Luck,

Brian G

这篇关于在Cocoa中解析NSXMLElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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