Javasript reads and writes XML

Source: Internet
Author: User

Load Method

For use
Indicates the file to be loaded from the specified position.
Basic syntax
Boolvalue = xmldocument. Load (URL );

Description
The URL contains the string of the URL of the file to be loaded. If the file is successfully loaded, the return value is true. If loading fails, the returned value is false.

Example
Boolvalue = xmldoc. Load ("lsta_1.xml ");
Alert (boolvalue );

Loadxml Method

For use
Load an XML file or a string segment.
Basic syntax
Boolvalue = xmldocument. loadxml (xmlstring );

Description
Xmlstring is a string that contains an XML text code.

Example
Xmlstring = "<greeting> <message> hello! </Message> </greeting> ";
Boolvalue = xmldoc. loadxml (xmlstring );
Alert (boolvalue );

Documentelement attributes

For use
Check the root node of the XML file.
Basic syntax
Objdoc=xmldocument.doc umentelement;

Description
Returns 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 umentelement;
Alert (objdocroot );

Childnodes attributes

For use
Returns a node list that contains all available subnodes of the node.
Basic syntax
Objnodelist = node. childnodes;

Description
Returns an object. If this node does not have a subnode, null is returned.

Example
Objnodelist = xmldoc. childnodes;
Alert (objnodelist );

Attribute attribute

For use
Returns the attribute list of the current node.
Basic syntax
Objattributelist = xmlnode. attributes;

Description
Returns an object. If this node cannot contain attributes, a null value is returned.

Example
Objattlist = xmldoc.doc umentelement. attributes;
Alert (objattlist );

Createnode Method

For use
Create a new node with a specified type, name, and namespace.
Basic syntax
Xmldocument. createnode (type, name, namespaceuri );

Description
Type is used to confirm the node type to be created. Name is a string to confirm the name of the new node. The namespace prefix is optional. Namespaceuri is a string that defines the namespace URI. If the prefix is included in the name parameter, this node is created with the specified prefix in the namespaceuri document. If the prefix is not included, the specified namespace is considered as the preset namespace.

Example
Objnewnode = xmldoc. createnode (1, "","");
Alert (objnewnode. XML );

Createtextnode Method

For use
Create a new text node and contain the specified data.
Basic syntax
Xmldocument. createtextnode (data );

Description
Data is a string representing the new text node. A new text node is created but not added to the file tree. To add a node to the file tree, you must use the insert method, for example, insertbefore, replaceChild, or appendchild.

Example
Objnewtextnode = xmldoc. createtextnode ("this is a text node .");
Alert (objnewtextnode. XML );

Createelement Method

For use
Creates an element with a specified name.
Basic syntax
Xmldocument. createelement (tagname );

Description
Tagname is a case-sensitive string to specify the name of a new element.

Example
Objnewelement = xmldoc. createelement ("");
Alert (objnewelement. XML );

Appendchild Method

For use
Add a node as the final sub-node of the specified node.
Basic syntax
Xmldocumentnode. appendchild (newchild );

Description
Newchild is the address for attaching a subnode.

Example
Docobj = xmldoc.doc umentelement;
Alert (docobj. XML );
Objnewnode = docobj.appendchild(xmldoc.doc umentelement. firstchild );
Alert (docobj. XML );

Removechild Method

For use
The specified node is removed from the node list.
Basic syntax
Objdocumentnode = xmldocumentnode. removechild (oldchild );

Description
Oldchild is a Node object that contains the node to be removed.

Example
Objremovenode = xmldoc.doc umentelement. childnodes. Item (3 );
Alert (xmldoc. XML );
Xmldoc.doc umentelement. removechild (objremovenode );
Alert (xmldoc. XML );

ReplaceChild Method

For use
Replace the specified old subnode as the new subnode.
Basic syntax
Objdocumentnode = xmldocumentnode. replaceChild (newchild, oldchild );

Description
Newchild is the object that contains the new subnode. If this parameter is null, the old subnode is removed and not replaced. Oldchild is the object that contains the old child node.

Example
Objoldnode = xmldoc.doc umentelement. childnodes. Item (3 );
Objnewnode = xmldoc. createcomment ("I 've replaced the BCC element .");
Alert (xmldoc. XML );
Xmldoc.doc umentelement. replaceChild (objnewnode, objoldnode );
Alert (xmldoc. XML );

Selectnodes Method

For use
Returns all nodes that match the provided pattern.
Basic syntax
Objdocumentnodelist = xmldocumentnode. selectnodes (patternstring );

Description
Patternstring is a string containing the XSL style. This method will return the node list object, including the nodes that match the style. If no matching node exists, an empty list is returned.

Example
Objnodelist = xmldoc. selectnodes ("/");
Alert (objnodelist. Item (0). XML );

Nodevalue attributes

For use
Returns the text related to the specified node. This is not a data value in an element, but a node-related and unparsed text, just like an attribute or a processing command.
Basic syntax
Varnodevalue = xmldocnode. nodevalue;

Description
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 umentelement. nodevalue;
Alert (varnodevalue );

Nextsibling attributes

For use
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 umentelement. childnodes. Item (1). nextsibling;
Alert (objsibling );

It's enough for a while.

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.