Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dom Vs Sax - creating Xmls

Tags:

c++

dom

xml

sax

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

like image 811
Idan Avatar asked Feb 25 '10 07:02

Idan


7 Answers

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.

like image 56
Pascal MARTIN Avatar answered Sep 30 '22 13:09

Pascal MARTIN


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++;

like image 45
Santhosh Kumar Tekuri Avatar answered Oct 02 '22 13:10

Santhosh Kumar Tekuri


  • DOM is used when your data fits in memory with a convienent API.
  • With SAX you're able to write huge amounts of data, SAX also provides higher performance.

You also might be interessted in What are the differences between SAX and DOM parser

and DOM and SAX Put to the Test

like image 40
stacker Avatar answered Sep 29 '22 13:09

stacker


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.

like image 32
Didier Trosset Avatar answered Oct 01 '22 13:10

Didier Trosset


DOM is the natural choice for creating documents.

like image 34
Kangkan Avatar answered Sep 30 '22 13:09

Kangkan


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")
      )
    )
  ;
}
like image 26
Tronic Avatar answered Oct 01 '22 13:10

Tronic


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.

like image 40
knatten Avatar answered Sep 29 '22 13:09

knatten