TouchXML 问题

发布于 2024-11-18 00:24:26 字数 1722 浏览 3 评论 0原文

我有以下 xml,需要使用 TouchXML 进行解析。

<?xml version="1.0"?>
<categories>
    <category0>
    <title>Alcoholic Drinks</title>
        <description>Buy beers, wines, sprits and champagne from the top online alocholic drink stores.&#xD;
                 Whatever your tipple you are sure to find a drinks supplier from our top shops below:
        </description>
        <status>1</status>
        <popularStatus></popularStatus>
        <order></order>
        <link>alcoholic-drinks</link>
        <id>1</id>
    </category0>
    <category1>
        <title>Art and Collectibles</title>
        <description>Are you looking to buy contemporary or fine art, or do you prefer to make your own artwork?&# 
                &#xD;
                Whether type of artwork or craft materials you are looking for, you are certain to find one of the shops below more than helpful:
        </description>
        <status>1</status>
        <popularStatus></popularStatus>
        <order></order>
        <link>art-and-collectibles</link>
        <id>2</id>
    </category1>
    <category2>
        <title>Auctions</title>
        <description>Are you looking for the UK's biggest and best Auction Sites?&#xD;
                The team at safebuyer.co.uk have scoured the web to find the UK's favourite auctions, so why wait, start your bidding now!
        </description>
        ...
        ...
        ...

我正在考虑从根节点创建两个循环以获取标题和链接,但无法弄清楚如何做到这一点。有人可以帮忙吗?

I have got following xml which I need to parse using TouchXML.

<?xml version="1.0"?>
<categories>
    <category0>
    <title>Alcoholic Drinks</title>
        <description>Buy beers, wines, sprits and champagne from the top online alocholic drink stores.
                 Whatever your tipple you are sure to find a drinks supplier from our top shops below:
        </description>
        <status>1</status>
        <popularStatus></popularStatus>
        <order></order>
        <link>alcoholic-drinks</link>
        <id>1</id>
    </category0>
    <category1>
        <title>Art and Collectibles</title>
        <description>Are you looking to buy contemporary or fine art, or do you prefer to make your own artwork?&# 
                
                Whether type of artwork or craft materials you are looking for, you are certain to find one of the shops below more than helpful:
        </description>
        <status>1</status>
        <popularStatus></popularStatus>
        <order></order>
        <link>art-and-collectibles</link>
        <id>2</id>
    </category1>
    <category2>
        <title>Auctions</title>
        <description>Are you looking for the UK's biggest and best Auction Sites?
                The team at safebuyer.co.uk have scoured the web to find the UK's favourite auctions, so why wait, start your bidding now!
        </description>
        ...
        ...
        ...

I am thinking to create two loops from root node in order to fetch title and link but coudnt figure out how to do it. Can anybody help please.

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

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

发布评论

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

评论(4

落叶缤纷 2024-11-25 00:24:26

如果您可以更改 XML 文件并使所有类别标签相同。您可以将所有...而不是...和...

这样就很容易解析。你只需要创建类别类,如果你有正确的 xml 解析代码,所有标签都会被自动解析。

If you can change your XML file and make all the category tag same. You can put all ... Instead of ... and ....

So that would be pretty easy to parse. You just need to make category class and all the tag would be parse automatically if you have correct xml parsing code.

触ぅ动初心 2024-11-25 00:24:26
CXMLNode *node;
for(i=0; i<10 ; i++){
    NSString *xpath = [NSString stringWithFormat:@"//category%d/title", i]; 
    NSArray *title = [[node nodesForXPath:xpath] stringValue];
}

使用上面的代码..

CXMLNode *node;
for(i=0; i<10 ; i++){
    NSString *xpath = [NSString stringWithFormat:@"//category%d/title", i]; 
    NSArray *title = [[node nodesForXPath:xpath] stringValue];
}

Use the above code..

空心空情空意 2024-11-25 00:24:26
CXMLElement *element;
NSArray *titleItems = [[NSArray alloc] initWithArray:[element nodesForXPath:@"//category" error:nil]];  
for(CXMLElement *item in titleItems){
NSString *title = [[item selectSingleNode:@"title"] stringValue];

}

注意:类别节点应该是重复的......

CXMLElement *element;
NSArray *titleItems = [[NSArray alloc] initWithArray:[element nodesForXPath:@"//category" error:nil]];  
for(CXMLElement *item in titleItems){
NSString *title = [[item selectSingleNode:@"title"] stringValue];

}

Note: category node should be repeating.....

把梦留给海 2024-11-25 00:24:26

下面的代码为您提供了一个字典,其中键是标题,数据是链接。当然,如果您的 XML 文档“很大”,这并不是最好的方法。

CXMLDocument *doc = [[[CXMLDocument alloc] initWithXMLString:theXML options:0 error:nil] autorelease];
NSArray *categories = nil;
NSMutableDictionary* results = nil;
categories = [doc nodesForXPath:@"/categories/*[starts-with(name(), 'category')]" error:nil];
if (categories != nil && [categories count] > 0)
{
         results = [NSMutableDictionary dictionaryWithCapacity:[categories count]];
         for (CXMLElement *category in categories)
         {
              NSArray* titles = [category elementsForName:@"title"];
              if ([titles count] >0)
              {
                   NSArray* links = [category elementsForName:@"link"];
                   [result setObject:([links count]>0?[[links objectAtIndex:0] stringValue]:nil;
                           forKey:[[titles objectAtIndex:0] stringValue]];
              } 
         }
}

The code below gives you a dictionary where keys are titles and data are the links. Of course, if your XML document is "big", this is not the best way to do it.

CXMLDocument *doc = [[[CXMLDocument alloc] initWithXMLString:theXML options:0 error:nil] autorelease];
NSArray *categories = nil;
NSMutableDictionary* results = nil;
categories = [doc nodesForXPath:@"/categories/*[starts-with(name(), 'category')]" error:nil];
if (categories != nil && [categories count] > 0)
{
         results = [NSMutableDictionary dictionaryWithCapacity:[categories count]];
         for (CXMLElement *category in categories)
         {
              NSArray* titles = [category elementsForName:@"title"];
              if ([titles count] >0)
              {
                   NSArray* links = [category elementsForName:@"link"];
                   [result setObject:([links count]>0?[[links objectAtIndex:0] stringValue]:nil;
                           forKey:[[titles objectAtIndex:0] stringValue]];
              } 
         }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文