0

My problem is that sendXML has more than one bracket in the beginning:

> sendXML: <<<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Table_info><table_id>1</table_id><action_id>3</action_id><Bestellliste><item><categorie>getraenke</categorie><item_id>102</item_id><menge>3</menge></item>item><categorie>getraenke</categorie><item_id>101</item_id><menge>2</menge></item>/Bestellliste></Table_info>

but it should be:

sendXML: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><Table_info><table_id>1</table_id><action_id>3</action_id><Bestellliste><item><categorie>getraenke</categorie><item_id>102</item_id><menge>3</menge></item><item><categorie>getraenke</categorie><item_id>101</item_id><menge>2</menge></item></Bestellliste></Table_info>

My code:

NSMutableArray * objectAttributes = [NSMutableArray arrayWithCapacity:100];

NSString *headerXML = [NSString stringWithFormat:
                                 @"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
                                 "<Table_info>"
                                 "<table_id>1</table_id>"
                                 "<action_id>3</action_id>"
                                 "<Bestellliste>"];

NSMutableArray *bodyXML = [NSMutableArray arrayWithCapacity:200];

NSString *endXML = [NSString stringWithFormat:
                             @"</Bestellliste>"
                             "</Table_info>"];

for(int i=0, j=0; i<getraenkeArray.count; i++)
{
    [objectAttributes addObject:[[getraenkeArray objectAtIndex:i] categorie]];
    [objectAttributes addObject:[[getraenkeArray objectAtIndex:i] item_id]];
    [objectAttributes addObject:[[getraenkeArray objectAtIndex:i] menge]];

    [bodyXML addObject:[NSString stringWithFormat:
                        @"<item>"
                        "<categorie>%@</categorie>"
                        "<item_id>%@</item_id>"
                        "<menge>%@</menge>"
                        "</item>",
               [objectAttributes objectAtIndex:j],
               [objectAttributes objectAtIndex:j+1],
               [objectAttributes objectAtIndex:j+2]]];
    j=j+3;
}


NSMutableString *sendXML = [NSMutableString stringWithCapacity:500];
int i=0;

[sendXML insertString: endXML atIndex: 0];  // Final-XML

// If no object is in getraenkeArray, bodyXML gets an empy standard-string
if (getraenkeArray.count == 0) {
    [bodyXML addObject:[NSString stringWithFormat:
                        @"<item>"
                        "<categorie></categorie>"
                        "<item_id></item_id>"
                        "<menge></menge>"
                        "</item>"]];
    [sendXML insertString: [bodyXML objectAtIndex:i] atIndex: i+1];
    NSLog(@"if");
}
else
{
    for(i=0; i<(getraenkeArray.count); i++)
    {
        [sendXML insertString: [bodyXML objectAtIndex:i] atIndex: i+1]; NSLog(@"i: %d", i);
    }
    NSLog(@"else");
}

[sendXML insertString: headerXML atIndex: i];
NSLog(@"XML: %@", sendXML);
3
  • Why are you not using an xml parser? Commented Apr 12, 2013 at 10:03
  • Because he's creating, not parsing XML. Commented Apr 12, 2013 at 10:11
  • This statement doesn't make much sense: [NSString stringWithFormat: @"</Bestellliste>" "</Table_info>"]. What are you trying to do with it? Commented Apr 12, 2013 at 10:34

1 Answer 1

1

In the for loop where you are inserting the items from your array into sendXML, you are inserting at index i+1, which is a character offset, so the second element is being inserted at character 1 of the first string, which is right after the first <, hence giving you the poor result,

Try either appendString: if you want the stings one after another in the sendXML string or getting the real insert location if you need to insert them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.