I know the difference between Sax and Dom is pretty substantial regarding parsing Xml, but what about creating ones ? is there even a way to create new Xml using Sax or that if i want to create new Xml file based on my data in my program , i will have to use DOM ?
Thanks
SAX is, quoting wikipedia :
SAX (Simple API for XML) is a serial access parser API for XML.
SAX provides a mechanism for reading data from an XML document.
i.e. SAX can be great for reading an XML document, but if you want to write one, you'll probably be using DOM.
You can use SAX to create XML. See following:
http://code.google.com/p/jlibs/wiki/XMLDocument
It is in Java. but the same can be achieved with C++;
You also might be interessted in What are the differences between SAX and DOM parser
and DOM and SAX Put to the Test
SAX is for reading XML documents, not writing them.
DOM on the other side is intended for creating a mapping between a structure in memory and an XML document.
If you already have your own data structure for your data in memory (that is much more convenient than DOM structure for your processing), creating a DOM data structure will mean to duplicate your data. Maybe that's not what you want if you have large data. In addition, you will have to create the DOM structure completely before you can write it to XML, doubling the memory size required for your application. Furthermore it will create latency in your processing.
I don't know about a library that would help writing an XML document, but if I was already using SAX, having my own data structure, I wouldn't bother with DOM.
DOM is the natural choice for creating documents.
I haven't seen SAX-style writing APIs, it is for reading only. For writing your options are DOM and various non-standardized builder APIs (that are often far more convenient to use than DOM). The non-standardized APIs differ from language/library to another, but see e.g. Builder for Ruby. I also drafted similar API for C++ with operator overloading: (the example is just testing a possible API, it doesn't actually do anything)
#include <string>
struct XMLBuilder {
XMLBuilder& operator[](std::string const& elemName) { return *this; }
XMLBuilder& operator()(std::string const& attrName, std::string const& attrValue) { return *this; }
XMLBuilder& operator()(std::string const& innerText) { return *this; }
XMLBuilder& operator()(XMLBuilder const& innerDoc) { return *this; }
};
int main() {
XMLBuilder()
["contact"](XMLBuilder()
["name"]("Patrick Hines")
["phone"]("type", "home")("206-555-0144")
["phone"]("type", "work")("425-555-0145")
["address"](XMLBuilder()
["street"]("...")
["zip"]("12345")
)
)
;
}
Sometimes I will just use printf to make simple XMLs, but usually it is worth it to use an XML-binding tool like e.g. Code Synthesis XSD. That will create a class hierarchy for you, based on an XSD. You just populate an object hierarchy, and it will create the XML for you.
In that way, you get static checking that your XML will be well formed and valid, which really helps to bring out the bugs early.
If you have ever used JAXB or any similar tool for Java, it is pretty much the same thing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With