In my understanding, given the same input that is an XML or an HTML document,
- If you use a DOM parser you will get a
DOM - If you use an HTML parser you will get a syntax tree, concrete or abstract
Here and there you will get a source that saying the result of parsing HTML is DOM (e.g. The HTML Parser Book), which I think not completely correct (though useful for those who don't care about the syntax tree). There are libraries distinguishing the two concepts, like hast-util-to-dom. So clearly the two are different.
But all of those links don't explain the differences between a DOM and a syntax tree.
1 Answer 1
DOM is not a syntax tree, it is a standard API that provides standard methods to work with the tree that represents the page contents.
The methods can not only read the tree, they can also modify it, causing the page contents to change.
-
$\begingroup$ DOM is an object model. Isn't that object model isn't an API? And I can surely have a lib that provides API to manipulate the syntax tree? $\endgroup$Ooker– Ooker2024年12月02日 12:36:48 +00:00Commented Dec 2, 2024 at 12:36
-
$\begingroup$ An object model is an API, as far as I know. $\endgroup$reinierpost– reinierpost2024年12月02日 13:28:32 +00:00Commented Dec 2, 2024 at 13:28
-
$\begingroup$ In that case, why isn't the syntax tree an object model as well? You can say it's a tree, but that tree must be represented as an object, thus we have an object model of the tree. DOM is exactly the same $\endgroup$Ooker– Ooker2024年12月03日 04:12:39 +00:00Commented Dec 3, 2024 at 4:12
-
$\begingroup$ Well, if you're working in an object-oriented language, and your syntax tree is represented in terms of objects in your language, then that will give you an object model for your syntax tree. Note there are a few assumptions there. But the methods in that model can be anything and do anything. DOM is a standard object model that is provided to you. It is not a library, it is a specification of what the methods in that library must be called and what they must do. $\endgroup$reinierpost– reinierpost2024年12月03日 10:49:28 +00:00Commented Dec 3, 2024 at 10:49
-
$\begingroup$ You say that DOM is not a syntax tree, but this comment says it is. What do you think about it? $\endgroup$Ooker– Ooker2024年12月05日 03:07:47 +00:00Commented Dec 5, 2024 at 3:07
DOMParserAPI -- as opposed to about concepts, so I'm not sure whether folks will consider it appropriate here. I will leave it up to others to judge. $\endgroup$