简体   繁体   English

如何使用字符串在Qt中使用套接字发送/接收XML数据?

[英]How to send/receive XML data with sockets in Qt using string?

I have a Qt TCP Server and Client program which can interact with each other. 我有一个Qt TCP Server和Client程序,它们可以相互交互。 The Server can send some function generated data to the socket using Qtextstream . 服务器可以使用Qtextstream将一些函数生成的数据发送到套接字。 And the Client reads the data from the socket using simple readAll() and displays to a QtextEdit. 客户端使用简单的readAll()从套接字读取数据,并显示到QtextEdit。

Now my data from Server side is huge (around 7000+ samples ) and I need the data to appear on the Client side instantaneously. 现在,我从服务器端获得的数据非常庞大(大约有7000多个示例),我需要数据立即显示在客户端上。 I have learned that using XML will help in my case. 我了解到使用XML对我而言会有所帮助。 So, I made an Qt XML Server and it generates the whole xml data into a .xml file. 因此,我制作了一个Qt XML服务器,它将整个xml数据生成为一个.xml文件。 I read the .xml file in Client side and I can get to display its contents. 我在客户端读取了.xml文件,然后可以显示其内容。 I used the DOM method for parsing. 我使用DOM方法进行解析。 But I get the data to display only when all the 7000+ samples have been generated on the Server side. 但是,只有在服务器端生成了所有7000多个样本后,我才能显示数据。

I need clarifications on these questions: 我需要澄清以下问题:

  1. How do I write each element of the XML Server side in to a String and send them through socket? 如何将XML Server端的每个元素写入字符串并通过套接字发送它们? I learnt tagName() can help me, but I have not been able to figure out how. 我了解到tagName()可以帮助我,但是我还无法弄清楚该怎么做。
  2. Is there any other way other than the String method to get a single element generated in the Server side to appear in the Client side. 除了String方法外,还有其他方法可以使Server端生成的单个元素出现在Client端。

PS: I am a newbie, forgive my ignorance. PS:我是新手,请原谅我的无知。 Thank you. 谢谢。

Most DOM XML parsers require a complete, well-formed XML document before they'll do anything with it. 大多数DOM XML解析器在执行任何操作之前都需要一个完整的,格式正确的XML文档。 That's precisely what you see: your data is processed only after all of the samples have been received. 这就是您所看到的:仅在收到所有样本后才处理您的数据。

You need to use an incremental parser that doesn't care about the XML document not being complete yet. 您需要使用一个增量解析器,该解析器并不关心XML文档是否尚未完成。

On the other hand: if you're not requiring XML for interoperability with 3rd party systems, you're probably wasting a lot of resources by using it. 另一方面:如果您不需要XML来与第三方系统进行互操作,那么使用XML可能会浪费大量资源。 I don't know where you've "learned" that XML will "help in your case". 我不知道您从哪里“了解”到XML将“为您提供帮助”。 To me it's not learning, it's just following the crowd without understanding what's going on. 对我来说,这不是学习,它只是在跟随人群,而不了解正在发生的事情。 Is your requirement to use XML or to move the data around? 您是否需要使用XML或移动数据? Moving data around has been a well understood problem for decades. 数十年来,移动数据一直是一个众所周知的问题。 Computers "speak" binary. 计算机“说”二进制。 No need to work around it, you know. 您知道,无需解决它。 If all you need is to move around some numbers, use QDataStream and be done with it. 如果您只需要移动一些数字,请使用QDataStream并完成此操作。 It'll be two orders of magnitude faster than the fastest XML parsers, you'll transmit an order of magnitude less data, and everyone will live happily ever after*. 它比最快的XML解析器快两个数量级,您将传输的数据量减少了一个数量级,每个人从此以后都会过上幸福的生活*。

*living happily ever after not guaranteed, individual results may vary. *从不保证自己过着幸福的生活,个别结果可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM