0

I'm quite new to programing and have a problem. I have been using touchxml for parsing and there hasn't been any problem before. Now i want to parse an xml string. I've looked all over the internet but can't find the answer.(i've never done initWithXMLString before maybe i'm doing something wrong here?)

My current code for parsing:

NSArray *resultNodes = NULL;
CXMLDocument *rssParser = [[CXMLDocument alloc] initWithXMLString:str options:0 error:nil]; 
NSString *strName;
resultNodes = [rssParser nodesForXPath:@"//FictionBook" error:nil];
NSLog(@"RESULT NODE COUNT =%d",[resultNodes count]);

and my string looks like this:

"<?xml version="1.0" encoding="UTF-8"?>
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink"><description><title-info><genre>prose_contemporary</genre> <author><first-name>Мария</first-name><last-name>Метлицкая</last-name><id>f97cbf85-bb7c-102b-8639-bb1d5f8374bd</id></author><book-title>Наша маленькая жизнь (сборник)</book-title> <annotation><p>Мария Метлицкая рассказывает о простых людях – они не летают в космос, не блистают на подмостках сцены, их не найдешь в списке Forbеs.</p></annotation></description></title-info></FictionBook>"

xml apears to be valid, I've checked about 10 xml validators.

But i get 0 from [resultNodes count].

Anybody encountered something similar before?

Thanks in advance!

Lukas
  • 2,515
  • 4
  • 28
  • 37
  • looking at your xml string and ends as , should the order of closing other way? – Saran Oct 20 '11 at 11:45
  • @Saran jap it should be, see my xml file is very big so i didn't want to post it all here, so i wrote the ending tags myself here. But i think the problem is somewhere else.. – Lukas Oct 20 '11 at 11:50
  • I hope you have some very good reason for not using NSXMLParser ! – Devarshi Oct 20 '11 at 12:26

2 Answers2

0

Try to check if there were an error while parsing:

NSError *error;
NSArray *resultNodes = NULL; 
CXMLDocument *rssParser = [[CXMLDocument alloc] initWithXMLString:str options:0 error:&error];
NSLog("Error: %@",error);

Also does your XML contains elements with namespaces?

negersiu
  • 552
  • 3
  • 20
  • well it throws an exc_bad_access when i try to log out the error, don't actually understand why this is happening. – Lukas Oct 20 '11 at 11:36
  • i will mark your answer as correct because you were the closest to answer of my problem, it was because of the elements with namespaces, because the root element had two of them i had to do it similar to this post: http://stackoverflow.com/questions/7295605/cxmldocument-cxmlnode-nodeforxpath-problem Thanks everyone! – Lukas Oct 20 '11 at 14:09
0

Firstly get you xml validate or not by this http://www.xmlvalidation.com/ and if it validated then you are not getting any response then you should check the error code what the error is thrown by the xml parser and then try to check by error code what is the problem.

B25Dec
  • 2,301
  • 5
  • 31
  • 54
  • thanks for trying to help! But i've already tried that, with no luck. Also i try to get the error like Maciulis offered NSError *error; NSArray *resultNodes = NULL; CXMLDocument *rssParser = [[CXMLDocument alloc] initWithXMLString:str options:0 error:&error]; NSLog("Error: %@",error); but then exc_bad_access comes.. – Lukas Oct 20 '11 at 11:39
  • do you getting any error code in the parser delegates ? if not please try the xml with nsxml parser with this demo it a good example at least you can identify the problem by different approaches . – B25Dec Oct 20 '11 at 11:42