Cocoa/Obj-C - 计算 XML 文件中的元素数量

发布于 2024-10-22 18:43:54 字数 1063 浏览 2 评论 0原文

有一个应用程序读取特定 XML 文件中的一些信息。 我想再添加一个选项,以下是描述:

这是我的 XML 类型文件的示例:

<?xml version="1.0" ?>
<Report>
    <ReportCreationDate>20110307162840</ReportCreationDate>
    <ReportTitle>Title sample</ReportTitle>
    <ReportDescription>Description sample</ReportDescription>
    <Videos>
        <Video>
            <VideoTitle>Video 1</VideoTitle>
            <VideoDescription>Video description sample</VideoDescription>
        </Video>
        <Video>
            <VideoTitle>Video 2</VideoTitle>
            <VideoDescription>Video description sample</VideoDescription>
        </Video>
    </Videos>
</Report>

我想计算 中有多少 元素<视频> 节点。
在此示例中,它是 2

我该怎么做?使用 NSXMLDocument?

我想将结果写入文本字段。

示例:
XML 文件包含 5 个视频。


如果有人可以提供帮助,那就太好了!
提前致谢
米斯基亚
(抱歉我的英语不好,我是法国人:p)

Got an application wich read some infos in a specific XML file.
I would like to add one more option, here is the description:

Here is an example of my XML type file:

<?xml version="1.0" ?>
<Report>
    <ReportCreationDate>20110307162840</ReportCreationDate>
    <ReportTitle>Title sample</ReportTitle>
    <ReportDescription>Description sample</ReportDescription>
    <Videos>
        <Video>
            <VideoTitle>Video 1</VideoTitle>
            <VideoDescription>Video description sample</VideoDescription>
        </Video>
        <Video>
            <VideoTitle>Video 2</VideoTitle>
            <VideoDescription>Video description sample</VideoDescription>
        </Video>
    </Videos>
</Report>

I would like to count how many <Video> elements is there in the <Videos> node.

For this example it's 2

How can I do that? Using NSXMLDocument?

I would like to write the result in a TextField.

Example:
XML file contains 5 videos.

If someone can help, it would be great!

Thanks in advance

Miskia

(and sorry for my poor english, i'm a frenchy :p)

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

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

发布评论

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

评论(1

愿得七秒忆 2024-10-29 18:43:54

像这样的事情应该可以完成工作。我将把错误处理留给你。

NSData *data = [NSData dataWithContentsOfFile:pathToYourXMLFile];

NSError *error = nil;

NSXMLDocument *document = [[NSXMLDocument alloc] initWithData:data options:0 error:&error];

if(!error)
{   
    NSXMLElement *rootElement = [document rootElement];

    NSUInteger count = [[rootElement nodesForXPath:@"//Video" error:&error] count];

    if(!error)
    {
        NSString *videosCount = nil;

        if(count == 0)
            videosCount = [NSString stringWithFormat:@"XML file contains no videos."];
        else if(count == 1)
            videosCount = [NSString stringWithFormat:@"XML file contains 1 video."];
        else
            videosCount = [NSString stringWithFormat:@"XML file contains %d videos.", count];

        [myTextField setStringValue:videosCount];
    }
}

[document release]

Something like this should get the job done. I'll leave the error handling up to you.

NSData *data = [NSData dataWithContentsOfFile:pathToYourXMLFile];

NSError *error = nil;

NSXMLDocument *document = [[NSXMLDocument alloc] initWithData:data options:0 error:&error];

if(!error)
{   
    NSXMLElement *rootElement = [document rootElement];

    NSUInteger count = [[rootElement nodesForXPath:@"//Video" error:&error] count];

    if(!error)
    {
        NSString *videosCount = nil;

        if(count == 0)
            videosCount = [NSString stringWithFormat:@"XML file contains no videos."];
        else if(count == 1)
            videosCount = [NSString stringWithFormat:@"XML file contains 1 video."];
        else
            videosCount = [NSString stringWithFormat:@"XML file contains %d videos.", count];

        [myTextField setStringValue:videosCount];
    }
}

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