ios - 用 XML 字符串填充数组

标签 ios xml xcode object nsmutablearray

我的问题是 sendXML 在开头有多个括号:

> 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>

但应该是:

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>

我的代码:

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);

最佳答案

在将数组中的项目插入到 sendXML 的 for 循环中,您在索引 i+1 处插入,这是一个字符偏移量,因此第二个元素被插入到第一个字符串的字符 1 处,即就在第一个 < 之后,因此给你的结果很差,

尝试 appendString:如果你想要sendXML中一个又一个的蜇伤字符串或获取真实的插入位置(如果需要插入它们)。

关于ios - 用 XML 字符串填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15968373/

相关文章:

iphone - 图片加载

javascript - 从 jQuery XML 对象到字符串的转换引发安全错误

java - web.xml 中的属性不起作用 eclipse kepler

iphone - 创建自定义 slider 的方法

ios - UIPageViewController 手势正在调用 viewControllerAfter : but doesn't animate

ios - 刷新 TableView iphone

.net - 将 XML 中的 <ArrayOf> 反序列化为 List<>

ios - 逆时标签

objective-c - 在 mac 包中嵌入字体

ios - 我应该在哪里从用户默认值加载数据?