Search code examples
iphonexmlreader

XML not parsing to NSDictionary?


I am using XMLReader for converting xmlString to NSDictionary and it is working fine to some of the xml string.But it is giving null dictionary for xml displayed below

<?xml version=\"1.0\"?>
<THEME>
    <OBJ>
        <FORMBG>0x00E0E0E0</FORMBG>
        <ALERTBG>0x00B4B4B4</ALERTBG>
        <FORMFONT>0x0033001A</FORMFONT>
        <FORMFONT2>0x00575757</FORMFONT2>
        <FORMFONT3>0x003E3E37</FORMFONT3>
        <ROWSELECTORLIST>0x0000CC33</ROWSELECTORLIST>
        <ROWDIVIDER>0x00BBBBBB</ROWDIVIDER>
        <SUBHEADER>0x00A8A8A8</SUBHEADER>
        <SELECTORCOMPONENT>0x00C2C2C2</SELECTORCOMPONENT>
        <FOOTER>0x00796767</FOOTER>
    </OBJ>
</THEME>

my conversion code is as follows:

NSString *stringURL=[NSString stringWithFormat:@"http://202.58.232.138/mt/theme.aspx"];
NSURL *url=[NSURL URLWithString:stringURL];

NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:url];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *responseString=[[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"response string is %@",responseString);

NSDictionary *dictionary=[XMLReader dictionaryForXMLString:responseString error:nil];

The url is printing following data on browser

    0x00E0E0E00x00B4B4B40x0033001A0x005757570x003E3E370x0000CC330x00BBBBBB0x00A8A8A80x00C2C2C2
0x00796767

and printing on xcode log will display following data.

<?xml version=\"1.0\"?>
<THEME>
    <OBJ>
        <FORMBG>0x00E0E0E0</FORMBG>
        <ALERTBG>0x00B4B4B4</ALERTBG>
        <FORMFONT>0x0033001A</FORMFONT>
        <FORMFONT2>0x00575757</FORMFONT2>
        <FORMFONT3>0x003E3E37</FORMFONT3>
        <ROWSELECTORLIST>0x0000CC33</ROWSELECTORLIST>
        <ROWDIVIDER>0x00BBBBBB</ROWDIVIDER>
        <SUBHEADER>0x00A8A8A8</SUBHEADER>
        <SELECTORCOMPONENT>0x00C2C2C2</SELECTORCOMPONENT>
        <FOOTER>0x00796767</FOOTER>
    </OBJ>
</THEME>

I think that's why it is not converting to NSDictionary. please help If you have faced such a problem before. When you pass above xml data to for [XMLReader dictionaryForXMLString:responseString error:nil]; at the place of response string then it will work perfectly.But if you execute above code it will display dictionary =(null).

Thanks in advance!


Solution

  • Finally found the mistake:

    it is counting escape sequence in response string.That's why not parsing to NSDictionary. I have added below line to replace escape sequence with blank.

    NSString *responseString=[[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
    responseString=[responseString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
    

    Now my code is running perfectly. output is showing below after parsing.

    xml dictianary is {
    THEME =     {
        OBJ =         {
            ALERTBG =             {
                text = 0x00B4B4B4;
            };
            FOOTER =             {
                text = 0x00796767;
            };
            FORMBG =             {
                text = 0x00E0E0E0;
            };
            FORMFONT =             {
                text = 0x0033001A;
            };
            FORMFONT2 =             {
                text = 0x00575757;
            };
            FORMFONT3 =             {
                text = 0x003E3E37;
            };
            ROWDIVIDER =             {
                text = 0x00BBBBBB;
            };
            ROWSELECTORLIST =             {
                text = 0x0000CC33;
            };
            SELECTORCOMPONENT =             {
                text = 0x00C2C2C2;
            };
            SUBHEADER =             {
                text = 0x00A8A8A8;
            };
        };
    };
    }