| Article Introduction: The Parsewithcontext () method is based on context resolution. |
Parsing based on context means parsing the string first, and then parsing the results into another document. The Parsewithcontext () method used at this time accepts 3 parameters: The Lsinput object, the context node, and the action to be performed. The StringData attribute of the Lsinput object must contain the code for the XML fragment and cannot contain the XML preamble. The context node is where the parse completion fragment should be inserted. The operation to be performed must be one of the following Lsparser constants.
- Action_append_as_children: Adds the parsing result as a child node to the context node.
- Action_replace_children: Removes all child nodes of the context node first, and then inserts the parse result as a child of the context node.
- Action_insert_before: Inserts the parsing result as a sibling of the context node in front of the context node.
- Action_insert_after: Inserts the parsing result as a sibling of the context node behind the context node.
- Action_insert_after: Replaces the context node with the parsed result.
In the case of a parse error, all of these actions are canceled. The following example shows the use of Parsewithcontext ():
var implementation = Document.implementation;
var parser = Implementation.createlsparser (implementation. Mode_synchronous, null);
var input = Implementation.createlsinput ();
Input.stringdata = "<root/>";
var xmldom = parser.parse (input);
var newinput = Implementation.createlsinput ();
Newinput.stringdata = "<child/>";
Parser.parsewithcontext (Newinput, xmldom.documentelement, parser. Action_append_as_children);
alert (xmldom.documentElement.firstChild.tagName); "Child"
After the above code executes the,<child> element will be called the child node of the <root> element. That is, the string "<child/>" will be parsed into an element and then inserted as a child element into the context child node. This parsing method effectively reduces the amount of code needed to create a DOM document fragment based on a string.