tags:

views:

426

answers:

8

Hey,

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

+1  A: 

DOM is the natural choice for creating documents.

Kangkan
+4  A: 

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.

Pascal MARTIN
the simplest possible way to create XML is to do byte/string level concatnation.. why people wants to use parsing API to create documents?
vtd-xml-author
Doing string concatenation, you'll have to deal with escaping yourself (including CDATA, for instance, that many people forget) ; which is already a good reason to use an API ;; Also, DOM is (pretty) standardize accros languages, which is a good thing, that will help making you code easier to understand ;; And I've seen so much crappy "concatenation"-type code used to create XML...
Pascal MARTIN
+2  A: 
  • 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

stacker
In addition, there are two more worth mentioning:*Pull or StaX*VTD-XML
vtd-xml-author
you need to sign in, in order to read the "DOM and SAX Put to the Test" link, this sucks..
idober
A: 

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")
      )
    )
  ;
}
Tronic
+2  A: 

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.

Didier Trosset
+2  A: 

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

Santhosh Kumar T
+1  A: 

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.

knatten
A: 

You could try finding an equivalent of .NET's XmlWriter in your language/platform.

Bart van Heukelom