views:

108

answers:

1

I have question about a basic xml file I'm parsing and just putting in simple nextlines(Enters). I'll try to explain my problem with this next example. I'm( still) building an xml tree and all it has to do ( this is a testtree ) is put the summary in an itemlist. I then export it to a plist so I can see if everything is done correctly.

A method that does this is in the parser which looks like this

if([elementName isEqualToString:@"Book"]) {
[appDelegate.books addObject:aBook];

[aBook release];
aBook = nil;
}
else
{
[aBook setValue:currentElementValue forKey:elementName];
NSString *directions = [NSString stringWithFormat:currentElementValue];
[directionTree = setObject:directions forKey:@"directions"];
}
[currentElementValue release];
currentElementValue = nil;
}

the export for the plistfile happens at the endtag of books.

Below is the first xmlfile

<?xml version="1.0" encoding="UTF-8"?>
<Books><Book id="1"><summary>Ero adn the ancient quest to measure the globe.</summary></Book><Book id="2"><summary>how the scientific revolution began.</summary></Book></Books>

This is my output

http://img139.imageshack.us/img139/9175/picture6rtn.png

If I make some adjustments like here

<?xml version="1.0" encoding="UTF-8"?>
<Books><Book id="1">
<summary>Ero adn the ancient quest to measure the globe.</summary>
</Book>

<Book id="2">
<summary>how the scientific revolution began.</summary>
</Book>
</Books>

My directions key with type string remains empty...

http://img248.imageshack.us/img248/5838/picture7y.png

I never knew that if I just put in an enter it would have such an influence. Does anyone know a solution to this since my real xml file looks like this.

ps. the funny thing is I can actually see ( when debugging)my directions string (NSString directions ) fill up with the currentElementValue in both cases.

+1  A: 

Instrument your code; specifically, just above the line that reads...

[directionTree setObject:directions forKey:@"directions"];

... (I removed a stray =) try adding ...

NSLog(@"setting directions to '%@'", directions);

I bet you'll see the above is logged multiple times per element. Specifically, the newline between the </summary> and the </book> tag is, in and of itself, an element just like the text in the <summary></summary> tag is an element.

Now, you could continue down the path of trying to special case for this that and the other, but that would be wrong.

You need to parse the XML as a structured document -- as a tree of nodes. Specifically, you should be looking for the <summary> tag somewhere and then grabbing the element that hangs below it (that should be of, IIRC, the TEXT type in XML parlance -- been a while).

Or, better yet, use one of the XML parsing APIs on the system. NSXMLDocument comes immediately to mind. If working on the iPhone (which this question didn't indicate), you'll need to use NSXMLParser and not NSXMLDocument as it is not available.

Or, even better, since this looks like pretty straightforward XML encapsulation of a regular data schema, use CoreData. CoreData is ideal for storing this kind of information. If your XML is intended to be an interchange format, you won't want to use CoreData as the XML it produces is entirely of its own design.

bbum
thnx for the respons..,indeed the "=" was just a typo ;). So you're saying that the newline is an element just like the text.. mmmok that makes sense indeed. So if i'm correct instead of using the Book tag I should use the summary tag instead. I'm using the xml parsing for iPhone and that means :" The iPhone only supports NSXMLParser and not NSXMLDocument" I'm still new to iPhone sdk and I saw something about coredata and was hoping I could avoid it;) and keep it simpeler;). But thanks a lot for the respons I'll definately check and see what I can do with it.
Yah-- right -- NSXMLDocument is right out in the context of the iPhone. The key, though, is that you need to treat the document like the tree of nodes that it is and not a stream of text.
bbum