Parsewithcontext () method based on context resolution

Source: Internet
Author: User

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.