在Cocoa中解析XML [英] Parsing XML in Cocoa
本文介绍了在Cocoa中解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个XML文件,我已经导入到Cocoa,并想要修改一个值。我怎么做 ?似乎没有什么好的例子。
我将这个文件导入到Cocoa(工作正常),现在想要更改imp的值,然后再将它传输到服务器。我怎么会这样做?感谢
I have this XML file that I have imported into Cocoa and want to modify one value. How do I do that ? There don't seem to be any good examples. I imported this file into Cocoa ( works properly )and now want to change the value of imp, before I transmit it to a server. How would I go about doing this ? Thanks
<?xml version='1.0'?>
<Root xmlns='http://www.abc.uk' version='1.0' name='full'>
<child1 version='2.0'>
<value1>
<user>abc</user>
<pass>xyz</pass>
</value1>
</child1>
<child2>
<imp>12345</imp>
</child2>
</Root>
我有这个解决方案,但它不能像我想要的那样工作。
I have this solution, but its not working as I would want it to.
xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:furl
options:NSXMLDocumentTidyXML error:&err];
NSArray *children = [[xmlDoc rootElement] children];
int i, count = [children count];
//loop through each child
for (i=0; i < count; i++)
{
NSXMLElement *child = [children objectAtIndex:i];
NSLog(@"child Name is %@", child.name);
if ([child.name isEqual:@"child2"])
{
[[child attributeForName:@"imp"] setStringValue:@"4567"];
break;
}
}
推荐答案
这样做:
NSXMLNode *new = [NSXMLNode elementWithName:@"imp" stringValue:@"12345"];
[child replaceChildAtIndex:0 withNode:new];
这篇关于在Cocoa中解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文