XMPP:如何解析 XMPPMessage 元素
XMPP: How to parse XMPPMessage element
我从 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)
修复搜索属性:元素是节点(如主体、结果等),而其他元素只是先前元素的属性。
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];
对于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)")
}
}
}
我从 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)
修复搜索属性:元素是节点(如主体、结果等),而其他元素只是先前元素的属性。
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];
对于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)")
}
}
}