Introduction to XMLHTTP and XMLDOC (I)

Source: Internet
Author: User
Tags processing instruction

XMLHTTP is an Activex component used by javascript clients to send requests to servers. It inherits this idea in many ajax frameworks. However, ajax is asynchronous, but XMLHTTP can also be used for asynchronous requests, the following describes some common XMLHTTP methods.

1: Open Method

It is defined:

XmlHttp. Open ("HTTP submission method", "request URL", "Asynchronous", "username", "password ")

Contains five parameters. The first three are necessary,
The last two are optional (provided when the server requires authentication ).
The parameter meanings are as follows:
Http-method: the communication method of HTTP, such as GET or POST.
Url: the URL of the server that receives XML data. The ASP or CGI program is usually specified in the URL.
Async: A boolean identifier indicating whether the request is asynchronous.
If it is an asynchronous communication method (true), the client will not wait for the server's response;
If it is synchronous (false), the client will not perform other operations until the server returns a message.
UserID, used for Server Authentication

Password, used for Server Authentication

The usage is as follows:

Var xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");

XmlHttp. open ("GET", "http://pengxiaojun.cnblogs.com", true ,"","");

After the Request object is initialized using the Open method, call the Send method to Send data

2: Send Method
The parameter type is Variant, which can be a string, DOM tree, or any data stream.
Data transmission methods can be synchronous or asynchronous.
In asynchronous mode, once the packet is sent, the Send process is terminated and the client performs other operations;
In synchronization mode, the client will not end the Send process until the server returns a confirmation message.

ReadyState attribute in XMLHTTP object
It can reflect the progress of the server when processing requests.
The client program can set corresponding event handling methods based on the status information.
Value description
0 Response object has been created, but the XML Document Upload process has not been completed
1. the XML document has been loaded.
2. the XML file has been loaded and is being processed.
3. Some XML documents have been parsed.
4. The file has been parsed and the client can accept the returned message.

The client receives the response through the attributes of the XMLHTTP object.
ResponseText: uses the returned message as a text string;
ResponseBody: uses the returned message as the content of the HTML document;
ResponseXML: regards the returned message as an XML document, which is used when the server response message contains XML data;
ResponseStream: treats the returned message as a Stream object.

Below I have collected MEMBERS OF THE XMLDOC object:

Document Object Attributes

The following lists available document Object Attributes and usage methods.

Async attributes
------------------
The async attribute indicates whether asynchronous download is allowed.

Basic Syntax:
Boolvalue = XMLdocument. async;
XMLdocument. async = boolvalue;
Description: The Boolean value is writable (read/write). If asynchronous download is permitted, the value is True; otherwise, the value is False.
Example:
XmlDoc. async = "false ";
Alert (xmlDoc. async );

Attribute
-------------------
Returns the attribute list of the current node.

Basic Syntax:
ObjAttributeList = xmlNode. attributes;
Returns an object. If this node cannot contain attributes, a null value is returned.
Example:
ObjAttList = xmlDoc.doc ument. lement. attributes;
Alert (objAttList );
------------------------------------------------------------------------------
[Note]
In this example, [object] is returned.
This is because the returned object cannot be expressed in text unless other object attributes are used,
This appendix contains several such examples. Although many object attributes are not very useful,
However, you will see in the appendix how to combine attributes and methods to get your desired results.
------------------------------------------------------------------------------

ChildNodes attributes
-----------------
Returns a node list that contains all available subnodes of the node.

Basic Syntax:
ObjNodeList = node. childNodes;
Returns an object. If this node does not have a subnode, null is returned.
Example:
ObjNodeList = xmlDoc. childNodes;
Alert (objNodeList );

Doctype Properties
-----------------
Returns a file type node that contains the DTD of the current file.
This node is a general file type declaration,
For example, node <! Doctype email system "LstA_2.dtd">,
The Node object named "EMAIL" is returned.

Basic Syntax:
Objdoctypew.xmldocument.doc type;
Returns an object. This attribute is read-only. If this file does not contain a DTD, null is returned.
Example:
ObjDocType = xmlDoc.doc type;
Alert (objDocType. nodeName );
Document. lement attributes

Check the Root node of the XML file.
----------------------------
Basic Syntax:
ObjDoc = xmldocumentdocument. element;
Description: return an object that contains data in a single root file element.
This attribute is readable/written. If the file does not contain the root node, null is returned.

Example:
ObjDocRoot = xmlDoc.doc ument. element;
Alert (objDocRoot );

FirstChild attributes
-----------------
Confirm the first child element in the current node.

Basic Syntax:
ObjFirstChild = xmlDocNode. firstChild;
Note: This attribute is read-only and returns an object. If the node does not contain the first child element, null is returned.
Example:
ObjFirstChild = xmlDoc.doc ument. lement. firstChild;
Alert (objFirstChild );

Implementation attributes
---------------------
DOM applications can use objects in other implementations.
The implementation attribute confirms the DOMimplementation object of the current XML file.

Basic Syntax:
ObjImplementation = xmldocument. implementation;
Description: This attribute is read-only and returns an object.
Example:
ObjImp = xmlDoc. implementation;
Alert (objImp );

LastChild attributes
---------------
Confirm the final child element of the current node.

Basic Syntax:
ObjLastChild = xmlDocNode. lastChild;
Description: This attribute is read-only and returns an object. If the node does not contain the last child element, null is returned.
Example:
ObjLastChild = xmlDoc.doc ument. lement. lastChild;
Alert (objLastChild );

NextSibling attributes
------------------
Return to the next sibling node in the subnode list of the current file node.

Basic Syntax:
ObjNextSibling = xmlDocNode. nextSibling;
Description: This attribute is read-only and returns an object. If the node does not contain any other related node, null is returned.
Example:
ObjSibling = xmlDoc.doc ument. lement. childNodes. item (1). nextSibling;
Alert (objSibling );

NodeName attribute
---------------
Returns a string that represents the name of the current node.

Basic Syntax:
StrNodeName = xmlDocNode. nodeName;
Returns a string. This attribute is read-only and returns the element name, attribute, or object reference.
Example:
StrNodeName = xmlDoc.doc ument. lement. nodeName;
Alert (strNodeName );

NodeType attributes
---------------
Identifies the DOM type of a node.

Basic Syntax:
NumNodeType = xmlDocNode. nodeType;
Description: This attribute is read-only and returns a value.

Valid values are of the following types:
1-ELEMENT
2-ATTRIBUTE
3-TEXT
4-CDATA
5-ENTITY REFERENCE
6-ENTITY
7-PI (processing instruction)
8-COMMENT
9-document.
10-document.nbsp; TYPE
11-document.nbsp; FRAGMENT
12-NOTATION
-------------------------------
Example:
NumNodeType = xmlDoc.doc ument. lement. nodeType;
Alert (numNodeType );

Nodevalue attributes
-----------------
Returns the text related to the specified node.
This is not a data value in an element, but a node-related and unparsed text,
It is like an attribute or a processing command.

Basic Syntax:
VarNodevalue = xmlDocNode. nodevalue;
Note: The returned text indicates the type value based on the node's nodeType attribute.
(See the nodeType attribute in the appendix .)
Because the node type may be one of several data types, the return value is also different.
The node types that return null include:
Document. ELEMENT, document. TYPE, document. FRAGMENT,
ENTITY, entity reference, and NOTATION.
This attribute can be erased.
Example:
VarNodevalue = xmlDoc.doc ument. lement. nodevalue;
Alert (varNodevalue );

Ondataavailable attributes
---------------------
Specify an event to process the ondataavailable event.
(For more information about ondataavailable events,
See the <document Object event> section in the appendix ).

Basic Syntax:
XmlDocNode. ondataavailable = value;
Note: This attribute is write-only and allows the file author to use the data as soon as possible once the data is available.
Example:
XmlDoc. ondataavailable = alert ("Data is now available .");

Onreadystatechange attribute
------------------------
Specify an event to handle the onreadystatechange event.
This event can identify changes to the readyState attribute.

Basic Syntax:
XmlDocNode. onreadystatechange = value;
Note: This attribute is write-only and allows the file author to specify a call event when the readyState attribute changes.
Example:
XmlDoc. onreadystatechange =
Alert ("The readyState property has changed .");

Ownerdocument attributes
------------------------
The root node of the file to be returned, including the current node.

Basic Syntax:
ObjOwnerDoc = xmldocument. ownerdocument.
Note: This attribute is read-only and returns an object containing the root node of the file, including a specific node.
Example:
ObjOwnerDoc = xmlDoc. childNodes. item (2). ownerdocument.
Alert (objOwnerDoc );

ParentNode attributes
-----------------
Returns the parent node of the current node. It can only be applied to nodes with a parent node.
 
Basic Syntax:
ObjParentNode = xmldocument. ode. parentNode;
Description: This attribute is read-only and returns the parent node object containing the specified node.
If the node does not exist in the file tree, null is returned.
Example:
ObjParentNode = xmlDoc. childNodes. item (1). parentNode;
Alert (objParentNode );

ParseError attributes
-----------------
Returns a DOM parsing error object that describes the last parsing error message.

Basic Syntax:
ObjParseErr = xmldocument. parseError;
Note: This attribute is read-only. If no error occurs, 0 is returned.
Example:
ObjParseErr = xmlDoc. parseError;
Alert (objParseErr );

Previussibling attributes
---------------------
Returns the sibling node before the current node.

Basic Syntax:
ObjPrevSibling = xmldocument. previussibling;
Returns an object. This attribute is read-only. If the node does not contain the previous sibling node, null is returned.
Example:
ObjPrevSibling = xmlDoc.doc ument. lement. childNodes. item (3). previousSibling
Alert (objPrevSibling );

ReadyState attributes
-------------------
Returns the current status of the XML file.

Basic Syntax:
IntState = xmldocument. readyState;
Note: This attribute is read-only,
Possible return values are as follows:
0-UNINITIALIZED: XML objects are generated, but no files are loaded.
1-LOADING: The LOADING program is in progress, but the file has not started parsing.
2-LOADED: some files have been LOADED and parsed, but the object model has not yet taken effect.
3-INTERACTIVE: only valid for some loaded files. In this case, the object model is valid but read-only.
4-COMPLETED: the file is fully loaded, indicating that the file is successfully loaded.
Example:
Alert ("The readyState property is" + xmlDoc. readyState );

Url attributes
---------------
Returns the URL of the last XML file to be loaded.

Basic Syntax:
StrDocUrl = xmldocument. url;
Note: This attribute is read-only and returns the URL of the last successfully loaded file,
If the file only exists in the primary storage (indicating that the file is not loaded by an external file), null is returned.
Example:
Alert (xmlDoc. url );

ValidateOnParse attributes
----------------------
Tells the parser whether the file is valid.

Basic Syntax:
BoolValidate = xmldocument. validateOnParse;
Xmldocument. validateOnParse = boolValidate;
Note: This attribute can be erased.
If the return value is true, the file is confirmed to be valid when it is parsed.
If false is returned, the file is invalid and is considered to be a standard (well-formed) file.
Example:
XmlDoc. validateOnParse = true;
Alert (xmlDoc. validateOnParse );

Xml attributes
------------
Returns the XML Description of the specified node and all child nodes.

Basic Syntax:
Xmlvalue = xmldocument. ode. xml;
Note: This attribute is read-only.
Example:
Xmlvalue = xmlDoc.doc ument. lement. xml;
Alert (xmlvalue );

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.