1

嗨,我正在使用 xmpp,一切正常。但我无法解析以下 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 是否等于广播。然后我需要解析<name>stream</name><value type="string">8d879bea44f792468d6ecad161d3e545</value> 如何在上面的 xml 中获取名称和值。我试过这样但每次条件都是成功的。下面是我的代码

 if ([message elementsForName:@"name"] ) {
    NSArray *nameArray=[message elementsForName:@"name"];
    NSLog(@"name Broadcast  %@",nameArray);

}

但 nameArray 总是空的。请帮帮我

4

1 回答 1

1

试试这个...... <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
}
于 2016-05-06T08:34:36.747 回答