Logic Machine Forum
Sending and receiving in XML - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Sending and receiving in XML (/showthread.php?tid=4638)



Sending and receiving in XML - palomino - 09.03.2023

Hello Daniel. 
You can send me one example of how I can send and receive format XML and Xsd.

this is the idea
I need to communicate with a server. 
To start the communication the server sends me information with this XML structure:
Code:
<?xml version="1.0" encoding="utf-8"?>
<stuMessages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://cody.glpconnect.com/XSD/StuMessage_Rev1_0.xsd" timeStamp="27/08/2009
21:00:00 GMT" messageID="56bdca48088610048fddba385e1cd5b8">
<stuMessage>
<esn>0-99990</esn>
<unixTime>1034268516</unixTime>
<gps>N</gps>
<payload length="9" source="pc" encoding="hex">0xC0560D72DA4AB2445A</payload>
</stuMessage>
</stuMessages>

I must read the message and send a response changing the  " timeStamp=" and  " messageID=".

This is the code that I have to send:
Code:
<?xml version="1.0" encoding="utf-8"?>
<stuResponseMsg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://cody.glpconnect.com/XSD/StuResponse_Rev1_0.xsd"
deliveryTimeStamp="25/08/2009 21:00:00 GMT" correlationID="56bdca48088610048fddba385e1cd5b8">
<state>pass</state>
<stateMessage>Store OK</stateMessage>
</stuResponseMsg>

the questions are:
How can I receive a message without sending a Request?
How can I send information in XML format?
How can I read and modify  XML format?

Tk for all your help.


RE: Sending and receiving in XML - Daniel - 10.03.2023

Have a look here how to receive data
https://forum.logicmachine.net/showthread.php?tid=4574&pid=29586#pid29586


RE: Sending and receiving in XML - admin - 10.03.2023

You can use lxp library to parse the XML:
Code:
xml = [[<?xml version="1.0" encoding="utf-8"?>
<stuMessages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://cody.glpconnect.com/XSD/StuMessage_Rev1_0.xsd" timeStamp="27/08/2009
21:00:00 GMT" messageID="56bdca48088610048fddba385e1cd5b8">
<stuMessage>
<esn>0-99990</esn>
<unixTime>1034268516</unixTime>
<gps>N</gps>
<payload length="9" source="pc" encoding="hex">0xC0560D72DA4AB2445A</payload>
</stuMessage>
</stuMessages>
]]

require('lxp').new({
  StartElement = function(parser, tag, attrs)
    if tag == 'stuMessages' then
      timestamp = attrs.timeStamp
      messageid = attrs.messageID
    end
  end
}):parse(xml)

log(timestamp)
log(messageid)

There's no library for creating XML. For simple formats you can use raw strings:
Code:
timestamp = '27/08/2009 21:00:00 GMT'
messageid = '56bdca48088610048fddba385e1cd5b8'

xml = [[<?xml version="1.0" encoding="utf-8"?>
<stuResponseMsg xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://cody.glpconnect.com/XSD/StuResponse_Rev1_0.xsd"
deliveryTimeStamp="]] .. timestamp .. [[" correlationID="]] .. messageid .. [[">
<state>pass</state>
<stateMessage>Store OK</stateMessage>
</stuResponseMsg>]]