如何解析 ios 中的 xmpp xml?
How to parse a xmpp xml in ios?
你好,我正在使用 xmpp,一切正常 fine.But 我无法在下面解析 xml
<message
xmlns="jabber:client" to="919452544@server" id="859e7-870" type="chat" from="917696997127@server/Smack">
<body>Broadcast</body>
<properties
xmlns="http://www.jivesoftware.com/xmlns/xmpp/properties">
<property>
<name>stream</name>
<value type="string">8d879bea44f792468d6ecad161d3e545</value>
</property>
<property>
<name>date</name>
<value type="string">2016-05-05 10:36:25</value>
</property>
<property>
<name>broadcast</name>
<value type="string">Broadcast</value>
</property>
</properties>
</message>
在上面 xml 首先我需要检查 body 是否等于 Broadcast。
然后我需要解析 <name>stream</name><value type="string">8d879bea44f792468d6ecad161d3e545</value>
如何获取上面xml中的name和value。
我这样试过,但每次条件都是 success.below 是我的代码
if ([message elementsForName:@"name"] ) {
NSArray *nameArray=[message elementsForName:@"name"];
NSLog(@"name Broadcast %@",nameArray);
}
但 nameArray 总是 empty.please 帮助我
试试这个……<name>stream</name>
是数组元素,<property>... <\property>
是 NSArray
NSXMLElement *properties = [message elementForName:@"properties"];
NSArray *property = [properties elementsForName:@"property"];
NSLog(@"property: %@", property);
//loop to get name and value
for (NSXMLElement *i in property)
{
NSXMLElement *name = [i elementForName:@"name"];
NSString *n = [name stringValue];
NSLog(@"name: %@", n);
// like wise you can get value
}
你好,我正在使用 xmpp,一切正常 fine.But 我无法在下面解析 xml
<message
xmlns="jabber:client" to="919452544@server" id="859e7-870" type="chat" from="917696997127@server/Smack">
<body>Broadcast</body>
<properties
xmlns="http://www.jivesoftware.com/xmlns/xmpp/properties">
<property>
<name>stream</name>
<value type="string">8d879bea44f792468d6ecad161d3e545</value>
</property>
<property>
<name>date</name>
<value type="string">2016-05-05 10:36:25</value>
</property>
<property>
<name>broadcast</name>
<value type="string">Broadcast</value>
</property>
</properties>
</message>
在上面 xml 首先我需要检查 body 是否等于 Broadcast。
然后我需要解析 <name>stream</name><value type="string">8d879bea44f792468d6ecad161d3e545</value>
如何获取上面xml中的name和value。
我这样试过,但每次条件都是 success.below 是我的代码
if ([message elementsForName:@"name"] ) {
NSArray *nameArray=[message elementsForName:@"name"];
NSLog(@"name Broadcast %@",nameArray);
}
但 nameArray 总是 empty.please 帮助我
试试这个……<name>stream</name>
是数组元素,<property>... <\property>
是 NSArray
NSXMLElement *properties = [message elementForName:@"properties"];
NSArray *property = [properties elementsForName:@"property"];
NSLog(@"property: %@", property);
//loop to get name and value
for (NSXMLElement *i in property)
{
NSXMLElement *name = [i elementForName:@"name"];
NSString *n = [name stringValue];
NSLog(@"name: %@", n);
// like wise you can get value
}