【问题标题】:XMPP: How to parse XMPPMessage elementXMPP:如何解析 XMPPMessage 元素
【发布时间】:2017-01-10 11:03:14
【问题描述】:

我为来自 xmpp 的每条消息获取以下 xml 格式

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

在控制台打印

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

我想从每条消息中获取“from”、“to”、“body”和“stamp”,我做了以下操作

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);

我只能打印消息正文和日志打印像

From: (null)
To: (null)
Body: hi
Stamp: (null)

【问题讨论】:

    标签: objective-c xmpp nsxmlelement


    【解决方案1】:

    修复搜索属性:元素是节点(如 Body、Result 等),而其他只是先前元素的属性。

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

    已编辑(抱歉,我最后一次使用 ObjectiveC 的工作太旧了)。

    xml namespace 是关于 element 而不是 attributes

    如果没有再次获取,请尝试通过 NSXMLNode 传递

    NSXMLNode *fromNode = [msg attributeForName:@"from"];
    NSString *from = [fromNode stringValue];
    

    【讨论】:

    • 它给出的错误 No Visible @interface for 'DDXMLElement' 声明了选择器 'attributeForName:xmlns:'
    • 刚刚从每一行中删除了 xmlns:@"jabber:client" 然后它的工作正常更新你的答案oooo
    • 你能告诉我如何从下面的 xml 中获取这些值 xdxz 7e396ecf-e446-4c01-9257-c61b3bf201f4
    • 评论太长了。将您的元素放入一个对象中(例如:NSXMLElement *forwa=[messageb elementForName:@"forwarded" xmlns:@"urn:xmpp:forward:0"]; 将包含从 ,然后获取像以前一样的元素或属性:)
    • @MrPk 如果你得到了问题的答案。请帮助我。stackoverflow.com/questions/44129790/…
    【解决方案2】:

    对于 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)")
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2017-02-02
      • 1970-01-01
      • 2023-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多