使用 TouchXML 获取 Cocoa 中元素的值
我使用 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>
我正在尝试获得 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 放在一个字符串中,但我对 Objective-C 的粗鲁介绍仍在继续...我刚刚意识到我已经被 .NET BCL 宠坏了,其中像 Regex 这样的东西是如此容易获得。
UPDATE2:我已经弄清楚如何使用 RegexKitLite 进行正则表达式。
所以现在的问题是,我如何从 FundValue 中获取“7800”,它现在是一个大字符串。 我已经通过使用 NSLog 写入它来确认我的 NSString 中有它。
I am using TouchXml because of the given limitations of NSXML on the actual iPhone. Anyway, I'm just starting out with Objective-C, I come from a C# background, and felt like learning something new..anyhow.. here is my xml file...
<?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>
I'm trying to get 7800, i.e FundValue. Can someone point me in the correct direction, I am using the TouchXml CXMLDocument and myParser is of type CXMLDocument, I have tried
NSArray *nodes = [myParser nodesForXPath:@"//FundInfo" error:&err];
Basically nodes evaluates to nil
if ([nodes count] > 0 ) {
amount = [nodes objectAtIndex:1];
}
UPDATE 1: I have abandoned parsing using XPath, and have replaced it with NSURLRequest, so now I have the entire XML in a string, but my rude introduction to objective-C continues...I just realized how spoiled I have become to the .NET BCL where things like Regex are so easily available.
UPDATE2: I've figured out how to use RegexKitLite for regex.
So the question now is, how do I get the "7800" from inside FundValue which is now one big string. I have confirmed I have it in my NSString by writing it using NSLog.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您的 XML 有一个名称空间,这意味着您的 XPath 实际上并不匹配。 不幸的是,没有默认映射,并且 XPath 实际上没有定义在内部处理此问题的好方法,因此您不能简单地通过更改 XPath 来修复它。 相反,您需要告知解释器您希望如何将其映射到 XPath 中。
看起来 TouchXML 通过以下方式实现了对此的支持:
因此您可以尝试以下操作:
The problem is that your XML has a namespace, which means your XPath does not actually match. Unfortunately there is no default mapping, and XPath does not actually define a good way to handle this internally, so you can't fix it simply by changing the XPath. Instead you need to inform the interpreter how you want to map it in your XPath.
It looks like TouchXML implements support for this via:
So you can try something like:
我有类似的问题。 对指定了命名空间 (xmlns="http://somenamespace") 的 XML 进行任何选择都将导致找不到节点。 非常奇怪的是,考虑到它确实支持其他命名空间格式,例如 xmlns:somenamespace="http://somenamespace"。
无论如何,到目前为止,最简单的事情就是进行字符串替换,并将 xmlns="http://tempuri.org/webservices 替换为空字符串。
总的来说,我喜欢 touchxml,但我可以不相信这个错误仍然存在。
I had a similar problem. ANY selection on an XML that has a namespace specified (xmlns="http://somenamespace") will result in no nodes found. Very strange considering it does support the additional namespace formats such as xmlns:somenamespace="http://somenamespace".
In any case, by far the easiest thing to do is do a string replace and replace xmlns="http://tempuri.org/webservices with an empty string.
Overall I like touchxml, but I can't believe this bug still exists.
尝试
Try