0

I'm getting the following xml format for every message coming from xmpp

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
 NSLog(@"Message %@",message);        
}

In console its printing

<message
    xmlns="jabber:client" from="10000006956@xmpp-dev.peeks.com" to="10000006956@xmpp-dev.peeks.com/mobile">
    <result
        xmlns="urn:xmpp:mam:tmp" id="1483781596098940">
        <forwarded
            xmlns="urn:xmpp:forward:0">
            <message
                xmlns="jabber:client" from="10000006931@xmpp-dev.peeks.com/mobile" to="10000006956@xmpp-dev.peeks.com" type="chat">
                <body>Hi</body>
                <delay
                    xmlns="urn:xmpp:delay" from="xmpp-dev.peeks.com" stamp="2016-12-22T04:50:17.023Z">Offline Storage
                </delay>
            </message>
            <delay
                xmlns="urn:xmpp:delay" from="xmpp-dev.peeks.com" stamp="2017-01-07T09:33:16.099Z">
            </delay>
        </forwarded>
    </result>
</message>

I want to fetch "from", "to", "body", and "stamp" from every message and i did the following

NSXMLElement *body = message;
    NSXMLElement *messageb  = [body elementForName:@"result" xmlns:@"urn:xmpp:mam:tmp"];
    NSXMLElement *forwa=[messageb elementForName:@"forwarded" xmlns:@"urn:xmpp:forward:0"];
    NSXMLElement *msg=[forwa elementForName:@"message" xmlns:@"jabber:client"];
    NSXMLElement *TD=[forwa elementForName:@"delay" xmlns:@"urn:xmpp:delay"];

    NSString *from=[[msg elementForName:@"from" xmlns:@"jabber:client"] stringValue];
    NSString *to=[[msg elementForName:@"to" xmlns:@"jabber:client"] stringValue];
    NSString *bodyq=[[msg elementForName:@"body"] stringValue];
    NSString *stmp=[[TD elementForName:@"stamp" xmlns:@"urn:xmpp:delay"] stringValue];
    NSString *final=[NSString stringWithFormat:@"From: %@\nTo: %@\nBody: %@\nStamp: %@",from,to,bodyq,stmp];
    NSLog(@"forwa %@", final);

I can able to print only the message body and the log prints like

From: (null)
To: (null)
Body: hi
Stamp: (null)
Mahesh Narla
  • 342
  • 2
  • 3
  • 20

2 Answers2

2

For swift 5:

func xmppStream(_ sender: XMPPStream, didReceive message: XMPPMessage) {
        if let msg = message.body {
            if let from = message.attribute(forName: "from")?.stringValue{
                print("msg: \(msg), \(from)")
            }
        }
    }
Athul Sethu
  • 387
  • 2
  • 4
  • 18
1

Fix to search for attributes: elements are the nodes (like Body, Result etc.) while others are just attributes of previous elements.

NSString *from=[[msg attributeForName:@"from"] stringValue];
    NSString *to=[[msg attributeForName:@"to"] stringValue];
    NSString *stmp=[[TD attributeForName:@"stamp"] stringValue];

Edited (sorry, my last work with ObjectiveC it's really old).

xml namespace it's about element and not attributes.

If you didn't get again, try passing by NSXMLNode

NSXMLNode *fromNode = [msg attributeForName:@"from"];
NSString *from = [fromNode stringValue];
MrPk
  • 2,862
  • 2
  • 20
  • 26
  • Its giving error No Visible @interface for 'DDXMLElement' declares the selector 'attributeForName:xmlns:' – Mahesh Narla Jan 10 '17 at 11:14
  • 2
    just removed xmlns:@"jabber:client" from every line then its worked fine update ur answer toooo – Mahesh Narla Jan 10 '17 at 11:19
  • Will you tell me how to fetch those values from below xml xdxz 7e396ecf-e446-4c01-9257-c61b3bf201f4 – Mahesh Narla Jan 10 '17 at 13:01
  • Too long for a comment. Put your element in an object (example: NSXMLElement *forwa=[messageb elementForName:@"forwarded" xmlns:@"urn:xmpp:forward:0"]; will contains from up to , then get the element or the attribute as before :) – MrPk Jan 11 '17 at 10:12
  • @MrPk If you got the answer of your problem. Please help me .https://stackoverflow.com/questions/44129790/how-to-parse-xmlns-message-in-swift3?noredirect=1#comment75280205_44129790 – San San May 23 '17 at 10:22