Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Originally posted by Mapraputa Is:
Yes, this is an important question. I was thinking about it last three days and did not feel that I can give satisfactory answer either. Was waiting for somebody with more practical experience to answer.
In my understanding, parsers are more "generic" tools. They operate on level of "elements", "attributes" etc. So you program would go like this: "Give me the root element. What is its name, by the way? Now give me all its immediate descendants. Is there an element with "sock" in its name?" You can process any XML document in this manner.
With binding APIs you have one particular type of XML documents, defined by DTD or Schema. These tools generate set of Java classes; roughly speaking elements are translated into classes and attributes into private fields. Then you compile these classes and now you can talk to your XML in terms of your object model, so instead of
currentElement.setAttributeValue("price", 10)
you can say:
sock.setPrice(10)
(warning: first example is contrived and employ neither DOM nor SAX syntax )
In both cases an input document has to be parsed, so I do not think the second model offers any improvement in efficiency. It frees you from learning DOM/SAX API, though.
Bottom line: XML is often defined as a meta-language to define domain-specific languages. In this model parsers are meta-tools to deal with this meta-language. Any particular instance of XML, however, isn't a meta-language. It uses limited vocabulary, and binding frameworks are tools to deal with these specific languages of limited vocabulary.
When would you use parser? Perhaps if you have too many different kinds of XMLs, so it's not sensible to have predefined set of classes for them. Or you do not know in advance what these XML documents can be, so you have to apply generic processing. Mapping one known database to XML fits none of these scenarios
Does it help?
[ February 12, 2002: Message edited by: Mapraputa Is ]
Originally posted by Mapraputa Is:
The binding tools are a big help in speeding development by automatically creating classes from schemas or DTD's, (even database schemas) and generating the code to do the marshalling/unmarshalling. The tools also save the novice, like me, from learning the details of SAX and/or DOM.
There are some cases when the parsers should be used directly like when you don't know in advance the struture of the XML doc.
I believe this is true.
This paper has a nice overview of XML binding (they call it "JAXB") vs. XML parsing (aka JAXP)
Quote:
"Use JAXB when you want to:
* Access data in memory, but do not need tree manipulation capabilities
* Process only data that is valid
* Convert data to different types
* Generate classes based on a DTD
* Build object representations of XML data.
Use JAXP when you want to:
* Have flexibility with regard to the way you access the data: either serially with SAX or
* randomly in memory with DOM
* Use your same processing code with documents based on different DTDs
* Parse documents that are not necessarily valid
* Apply XSLT transforms
* Insert or remove objects from an object tree that represents XML data"
Also, I have to say that other sources said generated classes *are* faster than old good parsing. I do not quite understand why, but it doesn't make my opponents view incorrect
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|