题
我使用的,因为NSXML的实际iPhone限制规定TouchXml。无论如何,我刚刚开始接触的Objective-C,我来自一个C#背景,感觉就像学习的东西new..anyhow ..这里是我的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>
我试图让7800,即FundValue。有人点我在正确的方向,我现在用的是TouchXml CXMLDocument和myParser的类型是CXMLDocument的,我已经试过
NSArray *nodes = [myParser nodesForXPath:@"//FundInfo" error:&err];
基本上节点的计算结果为无
if ([nodes count] > 0 ) {
amount = [nodes objectAtIndex:1];
}
更新1:我已经使用XPath放弃解析,并用的NSURLRequest取代它,所以现在我有一个字符串在整个XML,但我的粗鲁介绍客观-C继续...我只是意识到我是多么溺爱有成为了.NET基础类库里的东西一样正则表达式是如此容易获得。
UPDATE2:我已经找到了如何使用RegexKitLite的正则表达式。
所以,现在的问题是,我如何获得“7800”由内FundValue现在是一个大的字符串。我已确认我有它在我的NSString使用NSLog的写它。
解决方案
问题是,你的XML有一个命名空间,这意味着你的XPath实际上并不匹配。不幸的是没有默认映射,和XPath实际上并不定义在内部处理这一个很好的方法,所以你不能改变的XPath只是解决它。相反,你需要告知解释要如何在你的XPath映射。
它看起来像TouchXML实现经由该支持:
- (NSArray *)nodesForXPath:(NSString *)xpath namespaceMappings:(NSDictionary *)inNamespaceMappings error:(NSError **)error;
所以,你可以尝试这样的:
NSDictionary *mappings = [NSDictionary dictionaryWithObject:@"http://tempuri.org/webservices" forKey:@"tempuri"];
[myParser nodesForXPath:@"//tempuri:FundInfo" namespaceMappings:mappings error:&err];
其他提示
我也有类似的问题。在具有规定的命名空间(的xmlns =“HTTP:// somenamespace”)的XML的任何选择将导致在没有找到的节点。很奇怪考虑它支持额外的命名格式,如的xmlns:somenamespace =“HTTP:// somenamespace”。
在任何情况下,通过远到最容易的事情是做一个字符串替换和取代的xmlns =“http://tempuri.org/webservices与空字符串。
总的来说,我喜欢touchxml,但我不能相信这个漏洞仍然存在。
尝试
NSArray *nodes = [myParser nodesForXPath:@"/FundInfo/*" error:&err];