How to parse a xmpp xml in ios?

2019-07-28 02:08发布

问题:

Hi I am working with the xmpp every thing working fine.But I am unable to parse below 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>

in the above xml first i need to check that body is equal to Broadcast. Then i need to parse <name>stream</name><value type="string">8d879bea44f792468d6ecad161d3e545</value> How to get the name and value in above xml. I tried like this but every time condition is success.below is my code

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

}

but nameArray always empty.please help me out

回答1:

Try this ... <name>stream</name> is array element and <property>... <\property> is 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
}