Properti nodeValue digunakan untuk mendapatkan nilai teks dari sebuah node
Metode getAttribute() mengembalikan nilai atribut
Contoh
Contoh di bawah ini menggunakan buku file XML. xml
Sebuah fungsi, loadXMLDoc(), dalam JavaScript eksternal digunakan untuk memuat file XML
Dapatkan nilai elemen
Contoh ini menggunakan metode getElementsByTagname() untuk mendapatkan yang pertama
Get an attribute's value
This example uses the getAttribute() method to get the value of the "category" attribute of the first element in "books.xml".
Get the Value of an Element
In the DOM, everything is a node. Element nodes does not have a text value.
The text of an element node is stored in a child node. This node is called a text node.
The way to get the text of an element, is to get the value of the child node (text node).
Get an Element Value
The getElementsByTagName() method returns a node list containing all elements with the specified tag name in the same order as they appear in the source document.
The following code loads "books.xml" into xmlDoc using loadXMLDoc() and retrieves the first element:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0]; |
The childNodes property returns a list of child nodes. The element has only one child node. It is a text node.
The following code retrieves the text node of the element:
x=xmlDoc.getElementsByTagName("title")[0]; y=x.childNodes[0]; |
The nodeValue property returns the text value of the text node:
x=xmlDoc.getElementsByTagName("title")[0]; y=x.childNodes[0]; txt=y.nodeValue; |
Result: txt = "Everyday Italian"
Try it yourself
Loop through all elements: Try it yourself
Get the Value of an Attribute
In the DOM, attributes are nodes. Unlike element nodes, attribute nodes have text values.
The way to get the value of an attribute, is to get its text value.
This can be done using the getAttribute() method or using the nodeValue property of the attribute node.
Get an Attribute Value - getAttribute()
The getAttribute() method returns an attribute value.
The following code retrieves the text value of the "lang" attribute of the first element:
xmlDoc=loadXMLDoc("books.xml"); txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang"); |
Result: txt = "en"
Example explained:
- Load "books.xml" into xmlDoc using loadXMLDoc()
- Set the txt variable to be the value of the "lang" attribute of the first title element node
Try it yourself
Loop through all elements and get their "category" attributes: Try it yourself
Get an Attribute Value - getAttributeNode()
The getAttributeNode() method returns an attribute node.
The following code retrieves the text value of the "lang" attribute of the first element:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].getAttributeNode("lang"); txt=x.nodeValue; |
Result: txt = "en"
Example explained:
- Load "books.xml" into xmlDoc using loadXMLDoc()
- Get the "lang" attribute node of the first element node
- Set the txt variable to be the value of the attribute
Try it yourself
Loop through all elements and get their "category" attributes: Try it yourself
Learn XML with XML Editor - Free Trial!
oXygen helps you learn to define, edit, validate and transform XML documents. Supported technologies include XML Schema, DTD, Relax NG, XSLT, XPath, XQuery, CSS. Understand in no time how XSLT and XQuery work by using the intuitive oXygen debugger! Do you have any XML related questions? Get free answers from the oXygen XML forum and from the video demonstrations. Download a FREE 30-day trial today! |