JS Access XML node operation

Source: Internet
Author: User

Get element value

X=xmldoc.getelementsbytagname ("title") [0];
Y=x.childnodes[0];
Txt=y.nodevalue;
Result: txt = "Everyday Italian"

Get attribute Value-getattribute ()

Xmldoc=loadxmldoc ("books.xml");
Txt=xmldoc.getelementsbytagname ("title") [0].getattribute ("Lang");
Result: txt = "en"

Get attribute Value-GetAttributeNode ()

X=xmldoc.getelementsbytagname ("title") [0].getattributenode ("Lang");
Txt=x.nodevalue;

Change the value of a text node

xmldoc = Loadxmldoc ("books.xml");
x = xmldoc. getElementsByTagName ("title") [0].childnodes[0];
X.nodevalue= "Easy Cooking";

Change the value of a property

xmldoc = Loadxmldoc ("books.xml");
x = xmldoc. getElementsByTagName ("book");
X[0].setattribute ("category", "food");

Delete Element node

xmldoc = Loadxmldoc ("books.xml");
y = xmldoc.getelementsbytagname ("books") [0];
XmlDoc.documentElement.removeChild (y);

Delete itself

xmldoc = Loadxmldoc ("books.xml");
x = Xmldoc.getelementsbytagname ("book") [0];
X.parentnode.removechild (x);

Delete a text node

xmldoc = Loadxmldoc ("books.xml");
x = Xmldoc.getelementsbytagname ("title") [0];
y = x.childnodes[0];
X.removechild (y);

Emptying text nodes

xmldoc = Loadxmldoc ("books.xml");
x = Xmldoc.getelementsbytagname ("title") [0];
X.nodevalue = "";

Delete an attribute node by name

xmldoc = Loadxmldoc ("books.xml");
x = Xmldoc.getelementsbytagname ("book");
X[0].removeattribute (' category ');

To delete an attribute node from an object

xmldoc = Loadxmldoc ("books.xml");
x = Xmldoc.getelementsbytagname ("book");
for (Var i=0;i<x.length;i++) {
while (x[i].attributes.length>0) {
Attnode=x[i].attributes[0];
Old_att=x[i].removeattributenode (Attnode);
}
}
X[0].removeattribute (category);

Replace element node

The ReplaceChild () method is used to replace the node.
xmldoc = Loadxmldoc ("books.xml");
x = xmldoc.documentelement;

Create a book element, title element and a text node
NewNode = Xmldoc.createelement ("book");
Newtitle = xmldoc.createelement ("title");
NewText = Xmldoc.createtextnode ("A Notebook");

Add the text node to the title node,
Newtitle.appendchild (NewText);
Add the title node to the book node
Newnode.appendchild (Newtitle);

y = Xmldoc.getelementsbytagname ("book") [0];
Replace the first book node with the new node
X.replacechild (Newnode,y);

Replace data in a text node

The ReplaceData () method is used to replace data in a text node.
The ReplaceData () method has three parameters:
offset-where to start replacing characters. The offset value starts at 0.
length-How many characters to replace.
string-the string to be inserted.

xmldoc = Loadxmldoc ("books.xml");
x = Xmldoc.getelementsbytagname ("title") [0].childnodes[0];
X.replacedate (0,8, "easy");

Replace the data in the text node with the NodeValue property.
xmldoc = Loadxmldoc ("books.xml");
x = Xmldoc.getelementsbytagname ("title") [0].childnodes[0];
X.nodevalue= "Easy Italian";

Create a new element node

The CreateElement () method creates a new element node.
xmldoc = Loadxmldoc ("books.xml");
NewNode = Xmldoc.createelement ("edition");
x = Xmldoc.getelementsbytagname ("book") [0];
X.appendchild (NewNode);

Create a new attribute node

xmldoc = Loadxmldoc ("books.xml");
Newatt = Xmldoc.createattribute ("edition");
Newatt.nodevalue = "First";
x = Xmldoc.getelementsbytagname ("title");
X[0].setattributenode (Newatt);

Create properties from SetAttribute.
xmldoc = Loadxmldoc ("books.xml");
x = Xmldoc.getelementsbytagname ("book");
X[0].setattribute ("edition", "First");

Create a text node

The createTextNode () method creates a new text node.
xmldoc = Loadxmldoc ("books.xml");
NewNode = Xmldoc.createelement ("publisher");
NewText = Xmldoc.createtextnode ("中文版");
Newnode.appendchild (NewText);
x = Xmldoc.getelementsbytagname ("book") [0];
X.appendchild (NewNode);

Create a CDATA section node

xmldoc = Loadxmldoc ("books.xml");
Newcdata = Xmldoc.createcdatasection ("Special Offer & Book Sale");
x = Xmldoc.getelementsbytagname ("book") [0];
X.appendchild (Newcdata);

Create a Comment node

xmldoc = Loadxmldoc ("books.xml");
NewComment = xmldoc.createcomment ("Revised March 2008");
X=xmldoc.getelementsbytagname ("book") [0];
X.appendchild (NewComment);

Adding nodes

xmldoc = Loadxmldoc ("books.xml");
NewNode = Xmldoc.createelement ("publisher");
x = Xmldoc.getelementsbytagname ("book") [0];
X.appendchild (NewNode);

Insert Node--insertbefore ()

xmldoc = Loadxmldoc ("books.xml");
NewNode = Xmldoc.createelement ("book");
x = xmldoc.documentelement;
y = Xmldoc.getelementsbytagname ("book") [3];
X.insertbefore (Newnode,y);

Add new Property

xmldoc = Loadxmldoc ("books.xml");
x = Xmldoc.getelementsbytagname ("book");
X[0].setattribute ("edition", "First");

Add text to a text node--insertdata ()

xmldoc = Loadxmldoc ("books.xml");
x = Xmldoc.getelementsbytagname ("title") [0].childnodes[0];
X.insertdata (0, "easy");

Replication nodes

xmldoc = Loadxmldoc ("books.xml");
OldNode = Xmldoc.getelementsbytagname ("book") [0];
NewNOde = Oldnode.clonenode (true);
XmlDoc.documentElement.appendChild (NewNode);
Output all titles
y = xmldoc.getelementsbytagname ("title");
for (Var i=0;i<y.length;i++) {
document.write (Y[i].childnodes[0].nodevalue);
document.write ("<br/>");
}

Source: Http://blog.163.com/[email protected]/blog/static/4453605920115235839577/

JS Access XML node operation

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.