如何解析iOS中一个XMPP XML?(How to parse a xmpp xml in ios

2019-09-29 03:34发布

嗨,我与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第i个需要检查的身体等于广播。 然后我需要解析<name>stream</name><value type="string">8d879bea44f792468d6ecad161d3e545</value>如何获得在上面的XML的名称和值。 我想这样的,但每一次的条件是success.below是我的代码

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

}

但nameArray总是empty.please帮助我

Answer 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
}


文章来源: How to parse a xmpp xml in ios?