Not logged in. · Lost password · Register
Forum: MatriX RSS
Avatar
TijsVandeVelde #1
Member since Nov 2017 · 2 posts
Group memberships: Members
Show profile · Link to this post
Subject: Parsing custom message
Hi,

I have been using your sdk for a couple of days now for a personal project and sofar all is going well. I am using XEP-0313 to archive the messages on my server. I am capable of retrieving all messages sent from one user to an other.

This is the result i get per message in the xmppClient_OnMessage :

  1. <message from="fakeId" to="fakeId/MatriX" xmlns="jabber:client">
  2.  <result xmlns="urn:xmpp:mam:1" id="1511360750337715">
  3.     <forwarded xmlns="urn:xmpp:forward:0">
  4.       <delay xmlns="urn:xmpp:delay" stamp="2017-11-22T14:25:50.337715Z" />
  5.       <message xmlns="jabber:client" from="user1/MatriX" to="user2" type="chat">
  6.         <body>,kkkk</body>
  7.       </message>
  8.     </forwarded>
  9.  </result>
  10. </message>

I cannot seem to parse this to a class to be able to retrieve the body data and the delay data.

This is a quick example of the code i use to parse it.

  1. /// <summary>
  2. /// Chat log response.
  3. /// </summary>
  4. public class ChatLogResponse : XmppXElement
  5. {
  6.         #region Constructors
  7.         /// <summary>
  8.     /// Constructor.
  9.     /// </summary>
  10.     public ChatLogResponse() :base("jabber:client","message")
  11.     {}
  12.  
  13.     #endregion
  14.  
  15.     public string From
  16.     {
  17.         get { return GetTag("from"); }
  18.         set { SetTag("from", value); }
  19.     }
  20.    
  21.     public string To
  22.     {
  23.         get { return GetTag("to"); }
  24.         set { SetTag("to", value); }
  25.     }
  26.  
  27.     public ChatLogResponseResult Result
  28.     {
  29.         get { return Element<ChatLogResponseResult>(); }
  30.         set { Replace(value); }
  31.     }
  32. }
  33.  
  34.  
  35. public class ChatLogResponseResult : XmppXElement
  36. {
  37.     public ChatLogResponseResult() : base(Variables.MamProtocol, "result")
  38.     {}
  39.  
  40.     public int Id
  41.     {
  42.         get { return GetAttributeInt("id"); }
  43.         set { SetAttribute("id", value); }
  44.     }
  45.    
  46.     public ChatLogResponseForwarded Forwarded
  47.     {
  48.         get { return Element<ChatLogResponseForwarded>(); }
  49.         set { Replace(value); }
  50.     }
  51. }
  52.  
  53.  
  54. public class ChatLogResponseForwarded : XmppXElement
  55. {
  56.     public ChatLogResponseForwarded() : base("urn:xmpp:forward:0", "forwarded")
  57.     {
  58.     }
  59.    
  60.     public ChatLogResponseDelay Delay
  61.     {
  62.         get { return Element<ChatLogResponseDelay>(); }
  63.         set { Replace(value); }
  64.     }
  65.        
  66.     public ChatLogResponseMessage Message
  67.     {
  68.         get { return Element<ChatLogResponseMessage>(); }
  69.         set { Replace(value); }
  70.     }   
  71. }
  72.  
  73. public class  ChatLogResponseDelay : XmppXElement
  74. {
  75.     public ChatLogResponseDelay() : base("urn:xmpp:delay", "delay")
  76.     {}
  77.  
  78.     public string Stamp
  79.     {
  80.         get { return GetAttribute("stamp"); }
  81.         set { SetAttribute("stamp", value); }
  82.     }
  83. }
  84.  
  85. public class  ChatLogResponseMessage : XmppXElement
  86. {
  87.     public ChatLogResponseMessage() : base("jabber:client","message")
  88.     {}
  89.    
  90.     public string From
  91.     {
  92.         get { return GetAttribute("from"); }
  93.         set { SetAttribute("from", value); }
  94.     }
  95.    
  96.     public string To
  97.     {
  98.         get { return GetAttribute("to"); }
  99.         set { SetAttribute("to", value); }
  100.     }
  101.    
  102.     public string Type
  103.     {
  104.         get { return GetAttribute("type"); }
  105.         set { SetAttribute("type", value); }
  106.     }
  107.  
  108.     /// <summary>
  109.     /// Value child.
  110.     /// </summary>
  111.     public string Body
  112.     {
  113.         get { return GetTag("body"); }
  114.         set { SetTag("body",value); }
  115.     }
  116. }

I hope somebody sees what i am doing wrong.

Thank you in advance!
This post was edited on 2017-11-23, 23:36 by Alex.
Avatar
Alex #2
Member since Feb 2003 · 4327 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
most of the classes you wrote are obsolete, because they already exist in MatriX.

1) the outer message class is of type MatriX.Xmpp.Client.Message, as well as the inner message in your Xml.
2) The forwarded type is of  Matrix.Xmpp.MessageCarbons.Forwarded
3) The delay tag is f type  Matrix.Xmpp.Delay.Delay

The only element which which does not exist in MatriX is the result tag.

When you write custom Element classes you have to register them in teh Factory, have you done this?
Here is an example for this:
https://www.ag-software.net/matrix-xmpp-sdk/matrix-develop…
Avatar
TijsVandeVelde #3
Member since Nov 2017 · 2 posts
Group memberships: Members
Show profile · Link to this post
I greatly reduced my classes to parse the incomming message.
And i also forgot to register my own class in the factory.
Everything is working fine now.

Thank you for the information!
Avatar
Alex #4
Member since Feb 2003 · 4327 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
great to hear its working now.

The registration is required by parser for incoming packages. While it reads the XML and parsed out the tag name and namespace its requests a new class instance from the factory by tag name and namespace. If no registration exists it always creates an XmppXlement which is the base.

For outgoing packages the registration is not required.

Alex
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Forum: MatriX RSS