如何读取和解析 .xml 文件

发布于 2024-09-15 02:56:02 字数 524 浏览 2 评论 0 原文

我需要简单地读取 .xml 文件;像这样:

<exchanges>
    <exchange>
        <timestamp>2010-08-19 17:15:56</timestamp>
        <userid>Elijah-Woods-MacBook-Pro.local</userid>
        <clientname>elijah</clientname>
        <botid>Jarvis</botid>
        <input>firsthello</input>
        <response>Hello, sir. How may I help you?</response>
    </exchange>
</exchanges>

然后,解析“response”标签之间的所有内容。

以利亚

I need to simply read an .xml file; like this:

<exchanges>
    <exchange>
        <timestamp>2010-08-19 17:15:56</timestamp>
        <userid>Elijah-Woods-MacBook-Pro.local</userid>
        <clientname>elijah</clientname>
        <botid>Jarvis</botid>
        <input>firsthello</input>
        <response>Hello, sir. How may I help you?</response>
    </exchange>
</exchanges>

Then, parse whatever's in between the 'response' tag.

Elijah

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

姐不稀罕 2024-09-22 02:56:33

TouchXML 也是 iPhone 上一个非常好的 XML 库。它也被证明是解析最快的之一。

这是一个示例项目: http://dblog.com.au/general/iphone-sdk-tutorial-building-an-advanced-rss-reader-using-touchxml-part-1/

网上还有很多如果你擅长“touchxml iphone 教程”

TouchXML is also a very good library for XML on iPhone. It is been proved to be one of the fastest for parsing too.

Here is an examples project: http://dblog.com.au/general/iphone-sdk-tutorial-building-an-advanced-rss-reader-using-touchxml-part-1/

There are many more online if you good "touchxml iphone tutorial" though

街角卖回忆 2024-09-22 02:56:26

在 Cocoa 中解析 XML 有两种选择:

  1. 事件驱动,其中解析器发出委托类响应的事件。
  2. 基于树 ,解析器在其中构建您可以查询或修改的 XML 树。

事件驱动的解析占用的内存较少,但由于您没有构建整个 XML 树,因此无法使用 XPath 或 XQuery 来获取有关文档的信息。基于树的结构通常使用更多的内存(因为整个 XML 文档被转换为对象形式并存储在内存中),但它提供了更强大的机制来从树中获取数据。

You have two choices for parsing XML in Cocoa:

  1. Event-Driven, in which the parser emits events to which your delegate class responds.
  2. Tree-Based, in which the parser builds an XML tree that you can query or modify.

Event-driven parsing is less memory-intensive, but since you don't build a whole XML tree, you can't use XPath or XQuery to get information about the document. Tree-based generally uses more memory (since the entire XML document is converted into a object form and stored in memory), but it offers more powerful mechanisms for getting data out of the tree.

梨涡少年 2024-09-22 02:56:17

基本思想,代码不完整..基于GDataXML

http://code.google.com/p/gdata-objectivec-client/source/browse/trunk/Source/XMLSupport/

还可查看对多个解析器的分析

http://www.raywenderlich.com/553 /如何为您的 iPhone 项目选择最佳 xml 解析器

NSArray *entries = [doc nodesForXPath:@"//exchanges/exchange" error:nil];
for (GDataXMLElement *entry in entries) {
    NSMutableDictionary * dict = [[[NSMutableDictionary alloc]initWithCapacity:7 ] autorelease];
    [dict setValue:[[[entry elementsForName:@"timestamp"] objectAtIndex:0] stringValue] forKey:@"timestamp"];
    [dict setValue:[[[entry elementsForName:@"userid"] objectAtIndex:0] stringValue] forKey:@"userid"];
    [dict setValue:[[[entry elementsForName:@"clientname"] objectAtIndex:0] stringValue] forKey:@"clientname"];
    [dict setValue:[[[entry elementsForName:@"botid"] objectAtIndex:0] stringValue] forKey:@"botid"];
    [dict setValue:[[[entry elementsForName:@"input"] objectAtIndex:0] stringValue] forKey:@"input"];
    [dict setValue:[[[entry elementsForName:@"response"] objectAtIndex:0] stringValue] forKey:@"response"];

}

Basic idea, the code is not complete.. based on GDataXML

http://code.google.com/p/gdata-objectivec-client/source/browse/trunk/Source/XMLSupport/

also see this analysis of multiple parsers

http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project

NSArray *entries = [doc nodesForXPath:@"//exchanges/exchange" error:nil];
for (GDataXMLElement *entry in entries) {
    NSMutableDictionary * dict = [[[NSMutableDictionary alloc]initWithCapacity:7 ] autorelease];
    [dict setValue:[[[entry elementsForName:@"timestamp"] objectAtIndex:0] stringValue] forKey:@"timestamp"];
    [dict setValue:[[[entry elementsForName:@"userid"] objectAtIndex:0] stringValue] forKey:@"userid"];
    [dict setValue:[[[entry elementsForName:@"clientname"] objectAtIndex:0] stringValue] forKey:@"clientname"];
    [dict setValue:[[[entry elementsForName:@"botid"] objectAtIndex:0] stringValue] forKey:@"botid"];
    [dict setValue:[[[entry elementsForName:@"input"] objectAtIndex:0] stringValue] forKey:@"input"];
    [dict setValue:[[[entry elementsForName:@"response"] objectAtIndex:0] stringValue] forKey:@"response"];

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文