Dom Vs Sax - creating Xmls
Asked Answered
Y

9

6

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

Yun answered 25/2, 2010 at 7:27 Comment(0)
G
7

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.

Gog answered 25/2, 2010 at 7:37 Comment(2)
the simplest possible way to create XML is to do byte/string level concatnation.. why people wants to use parsing API to create documents?Handicraftsman
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...Gog
I
4

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

Illustrate answered 25/2, 2010 at 8:14 Comment(0)
C
2
  • 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

Cochleate answered 25/2, 2010 at 7:39 Comment(1)
you need to sign in, in order to read the "DOM and SAX Put to the Test" link, this sucks..Carmen
O
2

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.

Outcaste answered 25/2, 2010 at 8:10 Comment(0)
B
1

DOM is the natural choice for creating documents.

Broadway answered 25/2, 2010 at 7:31 Comment(0)
M
1

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")
      )
    )
  ;
}
Mcminn answered 25/2, 2010 at 7:43 Comment(0)
T
1

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.

Ticker answered 25/2, 2010 at 12:1 Comment(0)
F
0

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

Finegrained answered 2/7, 2010 at 22:55 Comment(0)
L
0

Always use DOM for creating new XML...For reading purpose prefer SAX..

Links answered 9/3, 2013 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.