JavaScript operations xml/html more commonly used object attributes collection _javascript tips

Source: Internet
Author: User
Tags unique id xpath xslt

Node Object properties

childnodes-returns the node list of nodes to child nodes

Firstchild-returns the first child node of the node.
Lastchild-returns the last child node of a node.

nextsibling-returns the sibling node immediately following the node.

nodename-returns the name of the node, depending on its type.
nodetype-returns the type of the node.
nodevalue-Sets or returns the value of a node, depending on its type.

ownerdocument-returns the root element (document object) of a node.

parentnode-returns the parent node of the node.

previoussibling-returns the sibling nodes immediately preceding the node.

text-returns the text of the node and its descendants (ie is unique).

xml-the XML that returns the node and its descendants (ie is unique).

Method of Node Object

AppendChild ()-Adds a new child node to the end of the node's child node list.

CloneNode ()-replicates nodes.

HasChildNodes ()-Determines whether the current node has child nodes.

InsertBefore ()-Inserts a new child node before the specified child node.

Normalize ()-merges adjacent text nodes and deletes empty text nodes.

RemoveChild ()-Deletes (and returns) the specified child node of the current node.

ReplaceChild ()-Replaces a child node with a new node.

IE6 Unique

SelectNodes ()-Selects a node with an XPath expression query.

selectSingleNode ()-finds a node that matches an XPath query.

Transformnode ()-Converts a node to a string using XSLT.

transformNodeToObject ()-Converts a node into a document using XSLT.

NodeList objects

length– returns the number of nodes in the list of nodes.

Item ()-Returns the node in the list of nodes that is at the specified index number.

For example:

JavaScript code

xmldoc = Loadxmldoc ("books.xml"); 
var x = xmldoc.getelementsbytagname ("title"); 

Output: Title Element:4

JavaScript code

var y = xmlDoc.documentElement.childNodes; 

Output: Book

NamedNodeMap objects

length-returns the number of nodes in the list.

getNamedItem ()-Returns the specified node. (by name)

Item ()-Returns the node at the specified index number.

RemoveNamedItem ()-Deletes the specified node (by name).

For example:

JavaScript code

xmldoc = Loadxmldoc ("books.xml"); 
var x = xmldoc.getelementsbytagname ("book"); 

Output: 1

JavaScript code

 
 

Output: Cooking

JavaScript code

 
 

Delete the Category property of the first book element

The Document object represents the entire XML file.

The properties of the Document object.

async-Specify whether the download of the XML file should be processed synchronously.

childnodes-returns a list of nodes that belong to the child nodes of the document.

doctype-returns document type declarations related to documents.

documentelement-returns the child nodes of the document.

Firstchild-returns the first child node of the document.

implementation-returns the Domimplementation object that processes the document. (ie not)

Lastchild-returns the last child node of the document.

nodetype-returns the node type.

nodename-returns its name based on the type of the node.

nodevalue-returns its value based on the type of the node.

text-returns the text of the node and its descendants (ie is unique).

xml-the XML that returns the node and its descendants (ie is unique).

Method of Document Object

CreateAttribute (Att_name)-Creates an attribute node with the specified name and returns a new Property object.
Createcdatasection (data)-Creates a CDATA section node.
Createcomment (data)-Creates a comment node.
Createdocumentfragment-creates an empty DocumentFragment object and returns this object.
CreateElement (Node_name)-Creates an element node.
Createentityreference (name)-Creates the EntityReference object and returns this object. (ie unique)
createTextNode (data)-Creates a text node.
getElementById (ElementID)-finds the element with the specified unique ID.
getElementsByTagName (Node_name)-Returns all element nodes with the specified name.

For example:

JavaScript code

 var xmldoc = Loadxmldoc ("Book.xml"); 
Xmldoc.async = false; 
var book = Xmldoc.getelementsbytagname ("book"); 
var newtext1= "Special offer & book Sale"; 
var newcdata=xmldoc.createcdatasection (NEWTEXT1); 
Book[0].appendchild (Newcdata); 
var newtext2= "revised September 2006"; 
var newcomment=xmldoc.createcomment (NEWTEXT2); 
Book[0].appendchild (NewComment); 
var var newel=xmldoc.createelement (' edition '); 
var newtext3=xmldoc.createtextnode (' a '); 
Newel.appendchild (NEWTEXT3); 
Book[0].appendchild (NEWEL); document.write ("<xmp>" + xmldoc.xml + "</xmp>"); 

Attributes of an Element object
attributes-returns the attributes of an element namednodemap
childnodes-returns the nodelist of the child node of the element
Firstchild-returns the first child node of the element.
Lastchild-returns the last child node of the element.
nextsibling-the node immediately after the element is returned.
nodename-returns the name of the node.
nodetype-returns the type of the element.
ownerdocument-returns the root element (document object) to which the element belongs.
parentnode-returns the parent node of the element.
previoussibling-the node immediately preceding the element return.
tagname-returns the name of the element.
text-returns the text of the node and its descendants. (ie-only)
xml-returns the XML for the node and its descendants. (ie-only)

Method of Element Object

AppendChild (node)-Adds a new child node to the end of the node's child node list.
CloneNode (True)-clones a node.
GetAttribute (Att_name)-Returns the value of the property.
GetAttributeNode (Att_name)-Returns the property node with an attribute object.
getElementsByTagName (Node_name)-finds descendant elements with the specified label name.
Hasattribute (Att_name)-Returns whether the element has the specified property.
HasAttributes ()-Returns whether an element has attributes.
HasChildNodes ()-Returns whether the element has child nodes.
InsertBefore (New_node,existing_node)-Inserts a new child node before an existing child node.
RemoveAttribute (att_name)-Deletes the specified property.
Removeattributenode (Att_node)-Deletes the specified attribute node.
RemoveChild (node)-Deletes child nodes.
ReplaceChild (New_node,old_node)-Replaces child nodes.
SetAttribute (Name,value)-Adds a new property or changes the value of the property.
SetAttribute (Att_node)-Adds a new property.

JavaScript code

X=xmldoc.getelementsbytagname (' book '); 
for (i=0;i<x.length;i++) {Attnode=x.item (i). GetAttributeNode ("category"); 
document.write (Attnode.name); 
document.write ("="); 
document.write (Attnode.value); 
document.write ("<br/>"); 
for (i=0;i<x.length;i++) {document.write (X[i].getattribute (' category ')); 
document.write ("<br/>"); 
} xmldoc=loadxmldoc ("/example/xdom/books.xml"); 
X=xmldoc.getelementsbytagname (' book '); 
document.write (X[0].getattribute (' category ')); 
document.write ("<br/>"); 
X[0].removeattribute (' category '); 
document.write (X[0].getattribute (' category ')); 
var attnode = X[1].getattributenode ("category"); 
var y = X[1].removeattributenode (Attnode); 
document.write ("<xmp>" + xmldoc.xml + "</xmp>"); 
 function Get_lastchild (n) {x = N.lastchild; 
 while (x.notetype!=1) {x = x.previoussibling; 
return x; 
 function Get_firstchild (n) {x = N.firstchild; 
 Whild (x.nodetype!=1) {x=x.nextsibling; 
return x; } xmldoc=Loadxmldoc ("books.xml"); 
X=xmldoc.getelementsbytagname ("book") [0]; 
Deleted_node=x.removechild (Get_lastchild (x));  document.write ("Node removed:" + deleted_node.nodename);

attr objects

The Attr object represents the properties of an Element object.

name-returns the name of the property.

nodename-returns the name of the node, based on its type

nodetype-returns the type of the node.

nodevalue-Sets or returns the value of a node, depending on its type

ownerdocument-returns the root element (document object) to which the property belongs.

Specified-returns True if the property value is set in the document, or FALSE if its default value is set in Dtd/schema.

value-Sets or returns the value of a property.

text-returns the text of the property. Ie-only.

xml-returns the XML of the property. Ie-only.

Properties of the text object

data-Sets or returns the text of an element or attribute.

length-returns the text length of an element or attribute.

Method of text Object

AppendData (String)-Append data to the node.

DeleteData (start,length)-deletes data from a node.

InsertData (start,string)-inserts data into the node.

ReplaceData (start,length,string)-replaces data in a node.

ReplaceData (offset)-divides a Text node into two.

Substringdata (start,length)-extracts data from a node.

About JavaScript Operations xml/html more commonly used object attributes to the collection of all the narration is over, more content please visit the Cloud Habitat Community website to learn more, thank you.

Related Article

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.