How to change attribute node in XML
In this articles I am going to explain about how to Change attribute node in XML DOM.
Get the value of an Attribute
-
DOM is a collection of node attributes, and DOM attribute node have a text value.
-
The getattribute() method using attribute node property
-
Attribute is the way to get the value of the text node.
-
This can be done using the getAttribute() method or using the nodeValue property of the attribute value.
-
The getAttribute() method returns an attribute value.
We change the attribute in a setattribute()
The setAttribute() method changes the value of attribute and create a new attribute.
Example
The following code change the attribute value:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="loadxmldoc.xml">
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc = loadXMLDoc("library.xml");
a = xmlDoc.getElementsByTagName('library');
a[0].setAttribute("category", "language");
document.write(a[0].getAttribute("category"));
</script>
</body>
</html>
|
Define the code
- First we create XML http object and open it.
- XML http script send the request to the server.
- "loadxmldoc.xml" get the data.
- Set the response as an XML DOM object.
- Setattribute() method set the category.
- Change the "category" attribute value to "language"
We change attribute node value
The nodeValue property can be used to change the value of a attribute node
Example
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="loadxmldoc.xml">
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc = loadXMLDoc("library.xml");
a = xmlDoc.getElementsByTagName("language")[0]
b = a.getAttributeNode("category");
b.nodeValue = "food";
document.write(b.nodeValue);
</script>
</body>
</html>
|
Define the code
- First we create XML http object and open it.
- XML http script send the request to the server.
- "loadxmldoc.xml" get the data.
- Set the response as an XML DOM object.
- Document. write is print the document
- getattributenode() method is change the attribute text.
You may also want to read these related articles :
Ask Your Question
Got a programming related question? You may want to post your question here
Programming Answers here