kd7*_*kd7 12 cocoa-touch touchxml
我正在使用TouchXml,因为NSXML在实际的iPhone上存在限制.无论如何,我刚刚开始使用Objective-C,我来自C#背景,感觉就像在学习一些新东西......但是......这是我的xml文件......
<?xml version="1.0" encoding="utf-8"?>
<FundInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/webservices">
<FundsReleaseDate>2009-02-11T00:00:00</FundsReleaseDate>
<FundValue>7800</FundValue>
<FundShares>1000</FundShares>
</FundInfo>
Run Code Online (Sandbox Code Playgroud)
我想获得7800,即FundValue.有人点我在正确的方向,我现在用的是TouchXml CXMLDocument和myParser的类型是CXMLDocument的,我曾尝试
NSArray *nodes = [myParser nodesForXPath:@"//FundInfo" error:&err];
Run Code Online (Sandbox Code Playgroud)
基本上节点的评估结果为零
if ([nodes count] > 0 ) {
amount = [nodes objectAtIndex:1];
}
Run Code Online (Sandbox Code Playgroud)
更新1:我已经使用XPath放弃解析,并用的NSURLRequest取代它,所以现在我有一个字符串在整个XML,但我的粗鲁介绍客观-C继续...我只是意识到如何撒娇我已经成为了.NET BCL,像Regex这样的东西很容易获得.
UPDATE2:我已经想通了如何使用RegexKitLite的正则表达式.
所以,现在的问题是,我该如何获得"7800"由内FundValue现在是一个大的字符串.我已确认我有它在我的NSString使用NSLog的写它.
Lou*_*arg 25
问题是你的XML有一个命名空间,这意味着你的XPath实际上并不匹配.不幸的是,没有默认映射,XPath实际上并没有定义一种在内部处理它的好方法,因此您无法通过更改XPath来修复它.相反,您需要通知解释器您希望如何在XPath中映射它.
看起来TouchXML通过以下方式实现对此的支持:
- (NSArray *)nodesForXPath:(NSString *)xpath namespaceMappings:(NSDictionary *)inNamespaceMappings error:(NSError **)error;
Run Code Online (Sandbox Code Playgroud)
所以你可以尝试类似的东西:
NSDictionary *mappings = [NSDictionary dictionaryWithObject:@"http://tempuri.org/webservices" forKey:@"tempuri"];
[myParser nodesForXPath:@"//tempuri:FundInfo" namespaceMappings:mappings error:&err];
Run Code Online (Sandbox Code Playgroud)