nicklockwood / XMLDictionary

[DEPRECATED]
http://charcoaldesign.co.uk/source/cocoa#xml-dictionary
Other
1.14k stars 235 forks source link

Empty tags are parsed into an array? #1

Closed Daniate closed 11 years ago

Daniate commented 11 years ago

If parse a tag like <ref></ref>,it's content is blank, it will be parsed into an array @[@""].I think, it need to be parsed into an empty string directly.

I do a few changes in the endText method.

- (void)endText
{
    if (TRIM_WHITE_SPACE)
    {
        self.text = (NSMutableString *)[text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    }
    if (text && [XML_TEXT_KEY length])
    {
        id existing = [self.top objectForKey:XML_TEXT_KEY];
        if (existing)
        {
            if (![text isEqualToString:@""])
            {
                if ([existing isKindOfClass:[NSMutableArray class]])
                {
                    [(NSMutableArray *)existing addObject:text];
                }
                else
                {
                    [self.top setObject:[NSMutableArray arrayWithObjects:existing, text, nil] forKey:XML_TEXT_KEY];
                }
            }
        }
        else
        {
            [self.top setObject:text forKey:XML_TEXT_KEY];
        }
    }
    self.text = [NSMutableString stringWithString:@""];
}

Best regards.

nicklockwood commented 11 years ago

I've not been able to reproduce this problem. Can you provide some sample XML demonstrating this issue?

nikotung commented 11 years ago

@nicklockwood here is the sample xml:

    <code><request>
    <Username>berrylai@suep.com</Username>
    <LastUpdateDate>2013-05-07 17:22:52</LastUpdateDate>
    <Storename>test</Storename>
    <Storetype></Storetype>
    <Storedescription></Storedescription>
    <Storelogo></Storelogo>
    </request></code>

then the dictionary from this xml is: { LastUpdateDate = "2013-05-07"; Storedescription = { "name" = Storedescription; }; Storelogo = { "name" = Storelogo; }; Storename = test; Storetype = { "name" = Storetype; }; Username = "test@test.com"; "name" = request; }

The tag ,Storedescription, Storelogo and Storetype,value is error in the dictionary.It should be Storelogo = "",Storename="",Storetype = "",right?

As I checked the code ,I found that you didn't handle it when the _text length is zero in endtext. - (void)endText { if (_trimWhiteSpace) { _text = [[_text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy]; } if ([_text length]) { NSMutableDictionary *top = [_stack lastObject]; id existing = top[XMLDictionaryTextKey]; if ([existing isKindOfClass:[NSArray class]]) { [existing addObject:_text]; } else if (existing) { top[XMLDictionaryTextKey] = [@[existing, _text] mutableCopy]; } else { top[XMLDictionaryTextKey] = _text; } } else { // HERE } _text = nil; }

Daniate commented 11 years ago

Nick, Really sorry, I was wrong. It was a dictionary instead of an array.

<root>     <xxx>1</xxx>     <xxx></xxx>     <xxx>2</xxx> <root>

it's result is, {     "name" = root;     root = {         "name" = root;     };     xxx = (         1,         {             "__name" = xxx;         },         2     ); }

After I changed the endText method, it's result is, {     "name" = root;     "text" = "";     root = {         "__name" = root;     };     xxx = (         1,         "",         2     ); }

I use version 1.0

nicklockwood commented 11 years ago

This should be fixed in version 1.3 - the behaviour of returning a dictionary for empty nodes was by design, but counterintuitive. I've added a new property "stripEmptyNodes" (on by default) that fixes this.