使用GDataXML在Objective C中更改xml中的属性值

时间:2013-06-14 11:09:53

标签: iphone ios ipad xml-parsing gdataxml

我想更改此XML的属性值

<btn1 btnID="1">
   <ch1 state="1" value="250" dimspeed="8" />
</btn1>

我想将状态值从0更改为1,反之亦然。另外,我想动态地改变“价值”和“减速”。

但我无法做任何选择。

我知道在PHP,android,C#等许多其他编程语言中都可以使用,但我在Objective c中找不到任何选项。我已经完成了2-3 xml解析器,现在我正在使用GDataXMLParser。有人可以帮帮我吗?

这是完整的XML文件

<units>
<unit mac="A0.36.119.80.203.67" ip="192.168.2.118" port="000001001" type="abc" name="cda">
<ch1 state="1" value="50" dimspeed="8" color="w">Channel 1</ch1>
<ch2 state="1" value="50" dimspeed="8" color="w">Channel 2</ch2>
<ch3 state="1" value="50" dimspeed="8" color="w">Channel 3</ch3>
<ch4 state="1" value="50" dimspeed="8" color="w">Channel 4</ch4>
<ch5 state="1" value="50" dimspeed="8" color="w">Channel 5</ch5>
<ch6 state="1" value="50" dimspeed="8" color="w">Channel 6</ch6>
<ch7 state="1" value="50" dimspeed="8" color="w">Channel 7</ch7>
<ch8 state="1" value="50" dimspeed="8" color="w">Channel 8</ch8>
<ch9 state="1" value="50" dimspeed="8" color="w">Channel 9</ch9>
<ch10 state="1" value="50" dimspeed="8" color="w">Channel 10</ch10>
<ch11 state="1" value="50" dimspeed="8" color="w">Channel 11</ch11>
<ch12 state="1" value="50" dimspeed="8" color="w">Channel 12</ch12>
<btn1 btnID="1">
   <ch1 state="1" value="250" dimspeed="8" />
   <ch2 state="1" value="250" dimspeed="8" />
   <ch3 state="1" value="250" dimspeed="8" />
   <ch4 state="1" value="250" dimspeed="8" />
   <ch5 state="1" value="250" dimspeed="8" />
   <ch6 state="1" value="250" dimspeed="8" />
   <ch7 state="1" value="250" dimspeed="8" />
   <ch8 state="1" value="250" dimspeed="8" />
   <ch9 state="1" value="250" dimspeed="8" />
   <ch10 state="1" value="250" dimspeed="8" />
   <ch11 state="1" value="250" dimspeed="8" />
   <ch12 state="1" value="250" dimspeed="8" />
</btn1>
<btn2 btnID="2">
   <ch1 state="1" value="187" dimspeed="8" />
   <ch2 state="1" value="187" dimspeed="8" />
   <ch3 state="1" value="187" dimspeed="8" />
   <ch4 state="1" value="187" dimspeed="8" />
   <ch5 state="1" value="187" dimspeed="8" />
   <ch6 state="1" value="187" dimspeed="8" />
   <ch7 state="1" value="187" dimspeed="8" />
   <ch8 state="1" value="187" dimspeed="8" />
   <ch9 state="1" value="187" dimspeed="8" />
   <ch10 state="1" value="187" dimspeed="8" />
   <ch11 state="1" value="187" dimspeed="8" />
   <ch12 state="1" value="187" dimspeed="8" />
</btn2>
<btn3 btnID="3">
   <ch1 state="1" value="125" dimspeed="8" />
   <ch2 state="1" value="125" dimspeed="8" />
   <ch3 state="1" value="125" dimspeed="8" />
   <ch4 state="1" value="125" dimspeed="8" />
   <ch5 state="1" value="125" dimspeed="8" />
   <ch6 state="1" value="125" dimspeed="8" />
   <ch7 state="1" value="125" dimspeed="8" />
   <ch8 state="1" value="125" dimspeed="8" />
   <ch9 state="1" value="125" dimspeed="8" />
   <ch10 state="1" value="125" dimspeed="8" />
   <ch11 state="1" value="125" dimspeed="8" />
   <ch12 state="1" value="125" dimspeed="8" />
</btn3>
<btn4 btnID="4">
   <ch1 state="1" value="125" dimspeed="8" />
   <ch2 state="1" value="125" dimspeed="8" />
   <ch3 state="1" value="125" dimspeed="8" />
   <ch4 state="1" value="125" dimspeed="8" />
   <ch5 state="1" value="125" dimspeed="8" />
   <ch6 state="1" value="125" dimspeed="8" />
   <ch7 state="1" value="125" dimspeed="8" />
   <ch8 state="1" value="125" dimspeed="8" />
   <ch9 state="1" value="125" dimspeed="8" />
   <ch10 state="1" value="125" dimspeed="8" />
   <ch11 state="1" value="125" dimspeed="8" />
   <ch12 state="1" value="125" dimspeed="8" />
</btn4>
<btn5 btnID="5">
   <ch1 state="1" value="125" dimspeed="8" />
   <ch2 state="1" value="125" dimspeed="8" />
   <ch3 state="1" value="125" dimspeed="8" />
   <ch4 state="1" value="125" dimspeed="8" />
   <ch5 state="1" value="125" dimspeed="8" />
   <ch6 state="1" value="125" dimspeed="8" />
   <ch7 state="1" value="125" dimspeed="8" />
   <ch8 state="1" value="125" dimspeed="8" />
   <ch9 state="1" value="125" dimspeed="8" />
   <ch10 state="1" value="125" dimspeed="8" />
   <ch11 state="1" value="125" dimspeed="8" />
   <ch12 state="1" value="125" dimspeed="8" />
</btn5>
<btn6 btnID="6">
   <ch1 state="1" value="125" dimspeed="8" />
   <ch2 state="1" value="125" dimspeed="8" />
   <ch3 state="1" value="125" dimspeed="8" />
   <ch4 state="1" value="125" dimspeed="8" />
   <ch5 state="1" value="125" dimspeed="8" />
   <ch6 state="1" value="125" dimspeed="8" />
   <ch7 state="1" value="125" dimspeed="8" />
   <ch8 state="1" value="125" dimspeed="8" />
   <ch9 state="1" value="125" dimspeed="8" />
   <ch10 state="1" value="125" dimspeed="8" />
   <ch11 state="1" value="125" dimspeed="8" />
   <ch12 state="1" value="125" dimspeed="8" />
</btn6>
</unit>

1 个答案:

答案 0 :(得分:2)

您可以通过执行以下操作来修改使用该XML构建的GDataXMLElement:

NSString *XMLString = @"<btn1 btnID=\"1\">\
<ch1 state=\"1\" value=\"250\" dimspeed=\"8\" />\
</btn1>";
NSError *error = nil;
GDataXMLElement *newElement = [[GDataXMLElement alloc] initWithXMLString: XMLString error: &error];
NSLog(@"New element: %@ error: %@", newElement, error);
if(nil == error)
{
  GDataXMLElement *childElement = [[newElement elementsForName: @"ch1"] objectAtIndex: 0];;
  [[childElement attributeForName: @"value"] setStringValue: @"500"];
}
NSLog(@"New element now: %@", newElement);

这会将名为“value”的属性的值从250更改为500,因为输出显示:

2013-06-14 12:51:12.406 TestApp[532:907] New element: GDataXMLElement 0x1f814790: {type:1 name:btn1 xml:"<btn1 btnID="1"><ch1 state="1" value="250" dimspeed="8"/></btn1>"} error: (null)
2013-06-14 12:51:16.223 TestApp[532:907] New element now: GDataXMLElement 0x1f814790: {type:1 name:btn1 xml:"<btn1 btnID="1"><ch1 state="1" value="500" dimspeed="8"/></btn1>"}