views:

321

answers:

2

Let me try to explain as clear as possible what I mean exactly with this question.

let's say we use this example http://www.iphonesdkarticles.com/2008/11/parsing-xml-files.html

and the xml looks instead of like this

<Books>
<Book id="1">
<title>Circumference</title>
<author>Nicholas Nicastro</author>
<summary>Eratosthenes and the Ancient Quest to Measure the Globe.</summary>
</Book>

<Book id="2">
<title>Copernicus Secret</title>
<author>Jack Repcheck</author>
<summary>How the scientific revolution began</summary>

</Book>

</Books>

It will look like this

<Books>
<Book id="1">
<title>Circumference</title>
<author>Nicholas Nicastro</author>
<summary id ='1'>Eratosthenes and the Ancient Quest to Measure the Globe.</summary>
<summary id ='2'>Eratosthenes more info in another tag.</summary>
<summary id ='3'>Eratosthenes and again another tag.</summary>
<summary id ='4'>Eratosthenes and the final tag another one here</summary>
</Book>

<Book id="2">
<title>Copernicus Secret</title>
<author>Jack Repcheck</author>
<summary id ='1'>How the scientific revolution began</summary>
<summary id ='2'>Eratosthenes more info in another tag.</summary>
<summary id ='3'>Eratosthenes and again another tag.</summary>
<summary id ='4'>Eratosthenes and the final tag another one here</summary>
</Book>

</Books>

Now if I follow the instruction on the site listed above , it doesn't explain how to handle summary 2,3,4( the xml i need to parse looks like that) and how I can show their output. All I will get is the last line. Does anyone have an idea about how I can get the other ones as well( meaning 2,3 in this case it seems to show only the last one since that's probably the last in the currentElementValue ).

I'm a bit confused would I have to address the attribute here as well or should I create a new search tag in my parser?

A: 

You will need to keep track of the summary elements you have parsed so far by keeping all the values in some container like an array: NSMutableArray. Thus, instead of an NSString to keep the summary, you'd have an NSMutableArray to hold the list of summaries you have parsed.

Whenever you encounter a summary in your parser, you don't set the NSString summary to the string you just read (which replaces the old value and explains why you only get the last summary tag). Instead, you store it as a new string and add that string to your NSMutableArray.

The problem with the design in the blog post you linked to is that it uses keyValueCoding to set the properties in the Book object and that doesn't facilitate adding items to an array item very well. Hence, you will need to include some special handling for the summary element in the parser and add methods to the Book class that allow you to add items to the summary array. See this post on how to also do that with KVC.

VoidPointer
thnx a lot .. I'm going to look into this... it's looks promising unfortunately I'm going to have to read the key coding document to get a better understanding I think;).
+2  A: 

I think this is what you need to be looking at, you could grab the value of the id field from the attributes and using that value assign it to a variable which you can then use.

So I might have something this in my didStartElement (where attributes is a variable declared in the header):

if([elementName isEqualToString:@"Summary"]){
    attributes = attributeDict;
}

Then something like this in my foundCharacters:

if([[attributes valueForKey:@"id"] intValue] == 1){
    doSomething
}else if([[attributes valueForKey:@"id"] intValue] == 2){
    doSomethingElse
}...

and so on until you've got all your data out.

N.B. This is 100% untested code but I'm confident it might work...

James Raybould
thnx a lot, though I have to say , it feels a bit mmm workaround like , I prefer a more efficient approach though I definately appreciate your input and may even use it if the other approach fails me.