What is the process to get value in XML DOM
In this articles i am going to explain about how to use XML DOM Get value.
DOM Get Node Values in XML
- DOM Get Node value is use to get the value of a text.
- The getAttribute() method is used to return the attribute value.
How to Get the value of an element
-
DOM is a collection of node, and DOM node do not have a text value.
-
The text node is store the value in a child node.
-
Node text is the way to get the value of the child node.
-
The getElementsByTagName() method returns a node list.
-
Node list containing all elements in same order as they appear source documents.
The nodeValue property returns the text value of the text node:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="loadxmldoc.html"></script>
</head>
<body>
<script type="text/javascript">
xmlDoc = loadXMLDoc("library.xml");
a = xmlDoc.getElementsByTagName("title")[0]
b = a.childNodes[0];
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.html" get the data.
- Set the response as an XML DOM object.
- Document. write is print the document
- Load file is read the text.
How to 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.
Example
The following code retrieves the text value of the "lang" attribute of the first <title> element:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="loadxmldoc.html">
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc = loadXMLDoc("books.xml");
txt = xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");
document.write(txt);
</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.html" get the data.
- Set the response as an XML DOM object.
- Document. write is print the document
- getattribute() method is read 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