0

xml以下に示す3レベルを解析する必要があります:-

<Navigation>
    <parent>
       <parentheader>
           <![CDATA[ Home ]]>
       </parentheader>
       <url>my-profile</url>
       <Content>...</Content>
    </parent>
    <parent>
       <parentheader>
           <![CDATA[ Exhibiton ]]>
       </parentheader>
           <child>
                <childheader>
                <![CDATA[ London Exhibition ]]>
                </childheader>
                    <subchild>London Sub
                         <url>ezone</url>
                         <Content>...</Content>
                    </subchild>
           </child>
           <child>
                <childheader>
                 <![CDATA[ Asia Exhibition ]]>
                </childheader>
                <url>exhibition-asia-tour</url>
               <Content>...</Content>
           </child>
      </parent>
</Navigation>

私はNSXMLParserクラスを実装しており、delegates以下のメソッドはコードです:-

.h ファイル

@interface NavigationXMLParser : NSObject<NSXMLParserDelegate>
{
    NSXMLParser *xmlParser;
    NSMutableDictionary *item,*childDict,*subChildDict;
    NSMutableArray  *nodesArr,*childArr,*subChildArr;
    NSMutableString *parent, *url,*child,*subchild;
    NSString *currentElement;
    BOOL childBool;
}

-(void) fetchXMLData;

@end

.m 実装ファイル コード:-

@implementation NavigationXMLParser

-(void) fetchXMLData
{

    xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://exhibitors.gastechkorea.com/admin/XMl_APP_Navigation.aspx"]];

    [xmlParser setDelegate:self];
    [xmlParser setShouldResolveExternalEntities:NO];
    [xmlParser setShouldProcessNamespaces:NO];
    [xmlParser setShouldReportNamespacePrefixes:NO];
    [xmlParser parse];
}

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    nodesArr =[[NSMutableArray alloc] init];

    //[sharedSQLiteObj createFavoriteAgendaTableNamed:@"" withField1:@"" withField2:@"" withField3:@"" withField4:@""];
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSString * errorString = [NSString stringWithFormat:@"Unable to download story feed from web site (Error code %i )", [parseError code]];
    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    {
        currentElement = [elementName copy];

        if ([elementName isEqualToString:@"parent"])
        {
            item = [[NSMutableDictionary alloc] init];
            parent = [[NSMutableString alloc]init];
            url = [[NSMutableString alloc]init];
        }
        else if ([elementName isEqualToString:@"child"])
        {
            child = [[NSMutableString alloc]init];
            childArr = [[NSMutableArray alloc] init];
            childDict = [[NSMutableDictionary alloc] init];
            url = [[NSMutableString alloc]init];

        }
        else if ([elementName isEqualToString:@"subchild"])
        {
            subchild = [[NSMutableString alloc]init];
            subChildArr = [[NSMutableArray alloc] init];
            subChildDict = [[NSMutableDictionary alloc] init];
            url = [[NSMutableString alloc]init];
        }
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if ([currentElement isEqualToString:@"parentheader"])
    {
        [parent appendString:string];
    }

    else if ([currentElement isEqualToString:@"url"])
    {
        [url appendString:string];
        if (!(child.length ==0))
        {
            if (subchild.length==0)
            {
                [childDict setObject:string forKey:@"url"];
            }
            else{
                [subChildDict setObject:string forKey:@"url"];
            }
        }
    }

   else if ([currentElement isEqualToString:@"childheader"])
    {
        [child appendString:string];

        if (!(string.length ==0))
        {
            [childDict setObject:string forKey:@"childheader"];
        }
    }
    else if ([currentElement isEqualToString:@"subchild"])
    {
        [subchild appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
        if (!([string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length ==0))
        {
            [subChildDict setObject:string forKey:@"subchild"];
            [subChildArr addObject:[subChildDict copy]];
            [childDict setObject:subChildArr forKey:@"subchild"];

            subChildDict = nil;
        };
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"child"])
    {
        if (!(childDict.count ==0)) {
            [childArr addObject:[childDict copy]];
            childDict = nil;
        }
    }
    if ([elementName isEqualToString:@"parent"])
    {
        [item setObject:[parent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] forKey:@"parent"];
        [item setObject:[url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] forKey:@"url"];
        if (!(childArr.count ==0))
        {
            [item setObject:childArr forKey:@"child"];
        }
        else
        {
            [item setObject:@"" forKey:@"child"];
        }
        //[item setObject:[subchild stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] forKey:@"subchild"];
        [nodesArr addObject:[item copy]];
    }
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    NSLog(@"nodesArr---%@",nodesArr);
}

@end

xmlparser クラスから応答配列を取得しました:-

(
        {
        child = "";
        parent = Home;
        url = "my-profile";
    },
        {
        child =         (
                        {
                childheader = "\n";
            }
        );
        parent = Exhibiton;
        url = "exhibition-asia-tour";
    },
        {
        child =         (
                        {
                childheader = "\n";
            }
        );
        parent = Calender;
        url = "http://www.google.com";
    }
)

私は正しい構造でデータを取得していません。どこか間違っていますが、解決策が見つかりませんでした。以下の構造でデータを取得したい:--

(
{ parent="…."
 child=""
url="……"
content="…."
}
{parent ="……"
child = ({ child="……";
          subchild= ({
                 name= "….."
                 url="….."
                 content="….."
            }
            {
                …………………….
                …..……………..
            })
           }
          {
        child="……"          
        ………………
           })
)
}

よろしくお願いします!!!

4

1 に答える 1

0

NSXMLParserDelegateメソッドをどのように利用するかがすべてです。ファイルXMLから次の情報を得ることができます。

[CDATA]の間の文字、および要素の間の文字。

したがって、ファイルDelegateから情報を取得するには、次のメソッドを実装する必要があります。XML

このメソッドは、次の間のすべての文字列を取得します[CDATA]

-(void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
    NSMutableString *str=[[NSMutableString alloc]initWithData:CDATABlock encoding:NSUTF8StringEncoding];
    [SomeArray addObject:str];
}

そして、このメソッドは要素間のすべての文字列を取得します:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    // here you need to alloc your string
    NSMutableString *parent = [[NSMutableString alloc]init];
    [parent appendString:string];
    [parent release];
}

NSMutableArrayおよびでデータを取得した後、必要に応じてデータNSMutableStringをフィルター処理できます。しかし、それはParseあなたのXMLファイルへの方法です。

それほど複雑ではありません。頑張ってください^_^

于 2013-05-29T10:48:42.500 に答える