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="……"
………………
})
)
}
よろしくお願いします!!!