tags:

views:

43

answers:

3

hi all, i am having an output xml which looks something like this,

    <?xml version="1.0"?>
<root>   
 <apple><apple1>A</apple1></apple>
    <ball><ball1>B</ball1></ball>
    <cat><cat1>C</cat1></cat>
    <dog><dog1>D</dog1></dog>
    <ele><ele1>E</ele1></ele>
<root>

i am using NSXmlParser, i m confused, is tgis a gud xml structure?>// secondly i dont knw how to parse this xml , i mean how should i write condition in parser didSTart and didEndElement ???

i m not to able properly navigate through each node, from apple to ele. i just want to assign the returned value of say apple to a string. same for each node.

i m so confused in if / else condition.

Suggestions are always appreciated

regards

+1  A: 

You need to have exactly one root element, like so:

<?xml version="1.0"?>
<root>
    <apple><apple1>A</apple1></apple>
    <ball><ball1>B</ball1></ball>
    <cat><cat1>C</cat1></cat>
    <dog><dog1>D</dog1></dog>
    <ele><ele1>E</ele1></ele>
</root>

I don't know about the specifics of the parser you are using, but this is an intrinsic XML issue.

Ioannis Karadimas
ohk, thats why i am able to navigate to only one node.. so ur xml format should right, a root tag at top and bottom.?/ thank loannis
shishir.bobby
Correct. It is necessary for XML to work. Glad to have helped.
Ioannis Karadimas
what about the if/else condition?? how to write that for each tag? thank loannis karamdimas
shishir.bobby
You might want to check out [ http://codesofa.com/blog/archive/2008/07/23/make-nsxmlparser-your-friend.html ]. I think that the [ (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string ] method should work for you.
Ioannis Karadimas
thanks mate, will give it a look.
shishir.bobby
A: 
jfalexvijay
hi thanks jfalexvijay for replying can u plz tell me what is itemDictionary and currentItem ??? and how can i pass returend attrubute values to NSSTRing, thank again for replying
shishir.bobby
A: 

Hi,

itemDictionary is a NSMutableDictionary.

I have edited my previous answer. (currentItem is itemDictionary). In this sample code, itemDictionary is to store all values with key (when we parsing the data).

itemDictionary - {
apple1 = A
ball1 = B
cat1 = C
dog1 = D
ele1 = E
}


//--------------------------
<?xml version="1.0"?>
<root>
<apple id='10'><apple1>A</apple1></apple>
<ball><ball1>B</ball1></ball>
<cat><cat1>C</cat1></cat>
<dog><dog1>D</dog1></dog>
<ele><ele1>E</ele1></ele>
<root>

In didStartElement method, you have to use below sample code.


if([elementName isEqualToString:@"apple"]){
    NSString *attributeValue = [attributeDict objectForKey:@"id"];
    NSLog("Attribute value : %@", attributeValue);
}

I hope, it will help you.

jfalexvijay