I didn't want to miss you. I just wanted a direct solution, but i wasn't sure about the best/correct approach.
Wrapping it is a good solution if you are going to switch over different solutions, but usually it causes an overhead. I'm not thinking on changing it at the moment, as it just works, and it's enough.
Anyway, your answer is a valid approach too. Sincerely, thank you very much for your kind support.
Best regards,
I also answered with a tiny (over simplified) code snippet that solves the problem with no major code changes and hence little danger. And will work with all output formats. I tend to wrap all of my 3rd party software like (like openssl, libxml2, libcsoap, etc) so that in theory I can replace one of them with something else and not affect my application. In theory. So the function that strips the header is part of my wrapper functions. E
On 8/22/2014 4:01 AM, Iñigo Martínez wrote:Yeah, it's ok, and the answer is totally valid. I only wondered if my approach had any known drawback. Thank you very much :) 2014-08-22 9:16 GMT+02:00 Daniel Veillard <veillard redhat com>:On Fri, Aug 22, 2014 at 08:34:37AM +0200, Iñigo Martínez wrote:It looks like a longer way. I mean, with the new saving API there is one more variable and one more function call, plus the creation of the full document, instead of using only just the nodes. Some code snippets. Using the saving API: xmlDocPtr doc; xmlNodePtr node; xmlBufferPtr buffer; xmlSaveCtxtPtr ctx; doc = xmlNewDoc (NULL); node = xmlNewNode (NULL, BAD_CAST "Command"); xmlDocSetRootElement (doc, node); xmlNewProp(node, BAD_CAST "Timestamp", BAD_CAST "0"); xmlNewProp(node, BAD_CAST "ReceptionTimestamp", BAD_CAST "0"); xmlNewProp(node, BAD_CAST "Sender", BAD_CAST "P1"); xmlNewProp(node, BAD_CAST "Receiver", BAD_CAST "P2"); xmlNewChild(node, NULL, BAD_CAST "GetData", NULL); buffer = xmlBufferCreate (); ctx = xmlSaveToBuffer (buffer, NULL, XML_SAVE_NO_DECL); if (xmlSaveDoc (ctx, doc) == -1) { fprintf (stderr, "Error saving buffer\n"); return; } xmlBufferFree (buffer); Using xmlNodeDump: xmlNodePtr node; xmlBufferPtr buffer; node = xmlNewNode (NULL, BAD_CAST "Command"); xmlNewProp (node, BAD_CAST "Timestamp", BAD_CAST "0"); xmlNewProp (node, BAD_CAST "ReceptionTimestamp", BAD_CAST "0"); xmlNewProp (node, BAD_CAST "Sender", BAD_CAST "P1"); xmlNewProp (node, BAD_CAST "Receiver", BAD_CAST "P2"); xmlNewChild (node, NULL, BAD_CAST "GetData", NULL); buffer = xmlBufferCreate (); if (xmlNodeDump(buffer, NULL, cmd, 0, 0) == -1) { fprintf (stderr, "Error saving buffer\n"); return; } xmlBufferFree (buffer); Is there any benefit on using the new saving API ?Depends on a variety of things I don't know like if your tree is always that simple, how much/often do you save, if you can reuse the ctx, if you can hook the ctx to your I/O, etc ... If the goal is purely to save to ram and discard it, yes it looks simpler You asked a question "how to save a doc without the XMLDecl ?", I answered, that's all ! Daniel2014-08-22 6:34 GMT+02:00 Daniel Veillard <veillard redhat com>:On Wed, Aug 20, 2014 at 07:04:06PM +0200, Iñigo Martínez wrote:Hi: I'm working with a protocol based on XML, something like: <Command Timestamp="0" ReceptionTimeStamp="0" Sender="P1" Receiver="P2"><GetData/></Command> I can successfully parse these messages using xmlReadMemory and navigating through the tree. When trying to create my own messages I can create them successfully this way: doc = xmlNewDoc(NULL); node = xmlNewNode(NULL, BAD_CAST "Command"); xmlDocSetRootElement(doc, node); xmlNewProp(node, BAD_CAST "Timestamp", BAD_CAST "0"); xmlNewProp(node, BAD_CAST "ReceptionTimestamp", BAD_CAST "0"); xmlNewProp(node, BAD_CAST "Sender", BAD_CAST "P1"); xmlNewProp(node, BAD_CAST "Receiver", BAD_CAST "P2"); xmlNewChild(node, NULL, BAD_CAST "GetData", NULL); The problem comes when trying to get the correspondant string as I get the xml version in the output: <?xml version="1.0"?><Command Timestamp="0" ReceptionTimeStamp="0" Sender="P1" Receiver="P2"><GetData/></Command> I have seen some tricks using a xmlTextWriter, but in my case I need an string not a file. Is there any way to avoid it ?Best way is to use the 'new' saving APIs http://xmlsoft.org/html/libxml-xmlsave.html#xmlSaveToBuffer with XML_SAVE_NO_DECL in the option and use http://xmlsoft.org/html/libxml-xmlsave.html#xmlSaveDoc if you are piling up such messages on an IO just use anothe xmlSaveTo... function and Flush to output instead of writing in memory and doing the I/O separately Daniel -- Daniel Veillard | Open Source and Standards, Red Hat veillard redhat com | libxml Gnome XML XSLT toolkit http://xmlsoft.org/ http://veillard.com/ | virtualization library http://libvirt.org/-- Daniel Veillard | Open Source and Standards, Red Hat veillard redhat com | libxml Gnome XML XSLT toolkit http://xmlsoft.org/ http://veillard.com/ | virtualization library http://libvirt.org/_______________________________________________ xml mailing list, project page http://xmlsoft.org/ xml gnome org https://mail.gnome.org/mailman/listinfo/xml
-- Eric S. Eberhard VICS 2933 W Middle Verde Road Camp Verde, AZ 86322 928-567-3727 work 928-301-7537 cell http://www.vicsmba.com/index.html (our work) http://www.vicsmba.com/ourpics/index.html (fun pictures)