简体   繁体   中英

Parsing XML data from Sockets using C

I have a client program which sends the XML data to my server program. The server needs to parse the XML data. I am using C language with Linux.

Is there any API available to parse XML from sockets directly? otherwise how could I know whether the XML transfer is completed or not?

As I tried to explain above: when you use "raw sockets", you'll read at most a buffer at a time. You won't necessarily read the whole document in one socket read; you might not even get a complete line. You need to copy the data a buffer at a time, in a loop, until you get all the text you need. This complicates parsing :)

Expat is a stream-oriented parser - that will probably help.

SAX is an "event driven" parser. You can use a SAX wrapper around Expat, you can also use any of many other choices, including libXML and Xerces.

The main point is that 1) reading your data (from the network), and 2) parsing your data are two separate activities .

For learning sockets, I'd strongly recommend Beej's Most Excellent "Network Programming Guide":

For learning Expat, here's a good tutorial:

'Hope that helps..

You don't parse data on sockets. You read(2) or recv(2) some data which can be parsed or processed after having been read or received (and these routines signal EOF eg by reading 0 bytes).

Several XML parsing libraries exist for C, see this answer .

In particular, many of them (expat, libxml2 , ...) are able to process pulled chunks of data. Concretely, you give them routines to be called on certain XML events (or when some XML element has been entirely parsed), you give them chunk reading routines, you install and start parsing, and eventually some of your XML event or element registered routines will be called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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