[iPhone] NSXMLParser problem

Discussion in 'Mac Programming' started by sujithkrishnan, Jun 3, 2008.

  1. sujithkrishnan macrumors 6502

    May 9, 2008
    Hi all.

    i am making us eof the NSXMLParser in my iPhone app.
    What i am facing now is that in my xml file i am having characters like

    <item>my favourites</item>
    <item>BOOKS & DVDs</item>
    <item>itemNameTM products</item>

    issue is that the foundCharacters method is returning me "BOOKS" , "&", "DVDs" in separately.

    Actually it should return :BOOKS & DVDs" as a single string, isnt it?

    the problem is there whereever i use "&", "trademark symbol" etc....
    Its giving "my favourites" as a single string, means the problem is not because of whitespaces.

    I tried appending all the strings that i got using appendString method of NSString class.

    But the resulting string is displayed as "Books....." .
    Its not because of the space limitation, i think the string is appending some miscs...

    Please help me...

    I got stuck up with my application...
  2. DerekS macrumors 6502

    Jun 25, 2007
    I don't think the & symbol is valid in an XML tag. I think you need to use:

  3. lazydog macrumors 6502a

    Sep 3, 2005
    Cramlington, UK
    Also, I think it's a good idea to not to rely on text to be contained in a single text node, ie you may need to concatenate sequential text nodes.

    b e n
  4. sujithkrishnan thread starter macrumors 6502

    May 9, 2008

    Yeah.. its actually '&amp' only when u open the file in DashCode, But in safari it will show as "&" only.

    So my question is why foundCharacters: method stops when some special chars get encoutered?

    ie... from <item>Books & DVDs</item>

    foundCharacters: method is returning "Books" , "&" , "DVDs" separately in 3 separate method calls.
    I am expecting entire string to get returned in a one call....
  5. iSee macrumors 68040


    Oct 25, 2004
    The documentation does state that a single text value can be returned in pieces. In this particular case I'd guess it is returning this in three separate pieces because "&" is represented in the XML as "&". The event driven parser is a low-level, low-overhead parser. It probably doesn't want to maintain any unnecessary state or do any unnecessary conversions. In this case, for example, it would have to convert the string "BOOKS & DVDs" (the way the characters exist in the source document) to "BOOKS & DVDs" (the way you would like to receive the text). The overhead of doing that conversion could add up, especially if there are large text nodes with a lot of entity references. It would be especially wasteful to do the conversion if the client doesn't need it (remember, NSXMLParser is a general class so it doesn't know whether or not it would help you to combine the parts for you).

    Anyway, there are surely other, less obvious reasons the parser might break a text node value apart. It's an implementation detail that you can't predict. The behavior could also change in a future update.

    So you're stuck. Your code will have to handle the concatination itself.
  6. psingh01 macrumors 65816

    Apr 19, 2004
    this is how the parsers in java work as well. they don't always return one string and you have to concatenate the strings. using a stack to keep track of the element you are in will do the trick.

Share This Page