1

我想改变这个 XML 的属性值

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

我想动态地将状态值从 0 更改为 1,反之亦然。我也想动态地改变“值”和“dimspeed”。

但我无法选择这样做。

我知道在 PHP、android、C# 等许多其他编程语言中是可能的,但我在目标 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>
4

1 回答 1

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>"}
于 2013-06-14T11:52:23.747 回答