XML DOM Knoten Informatie
- Previous page DOM access node
- Next page DOM node list
Knooppunt attributes: nodeName, nodeValue en nodeType.
Voorbeeld
De volgende voorbeelden gebruiken XML-bestanden books.xml.
De functie loadXMLDoc(), gelokaliseerd in extern JavaScript, wordt gebruikt om XML-bestanden te laden.
- Verkrijg de naam van het elementknooppunt
- Dit voorbeeld gebruikt het attribute nodeName om de naam van het wortelknooppunt in "books.xml" te verkrijgen.
- Verkrijg de tekst van de tekstknooppunt
- Dit voorbeeld gebruikt het attribute nodeValue om de tekst van het eerste <title> element in "books.xml" te verkrijgen.
- Wijzig de tekst in de tekstknooppunt
- Dit voorbeeld gebruikt het attribute nodeValue om de tekst van het eerste <title> element in "books.xml" te wijzigen.
- Verkrijg de naam en het type van het elementknooppunt
- Dit voorbeeld gebruikt de attributes nodeName en nodeType om de naam en het type van het wortelknooppunt in "books.xml" te verkrijgen.
Het attribute van het knooppunt
In de XML Document Object Model (DOM), is elk knooppunt eenObjecten.
Objecten hebben methoden (functies) en attributes (informatie over het object), en kunnen worden geaccesseerd en bewerkt via JavaScript.
Drie belangrijke XML DOM knooppunt attributes zijn:
- nodeName
- nodeValue
- nodeType
Het attribute nodeName
Het attribute nodeName definieert de naam van het knooppunt.
- nodeName is enkelvoudig gelezen
- De naam van het knooppunt van het element is het same als de naam van het element
- De naam van het knooppunt van het attribute is de naam van het attribute
- De naam van het knooppunt van de tekst is altijd #text
- De naam van het knooppunt van het document is altijd #document
Het attribute nodeValue
Het attribute nodeValue definieert de waarde van het knooppunt.
- The nodeValue of the element node is undefined
- The nodeValue of the text node is the text itself
- The nodeValue of the attribute node is the value of the attribute
Example 1: Get the value of the element
The following code retrieves the value of the text node of the first <title> element:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; txt=x.nodeValue;
Result: txt = "Harry Potter"
Code explanation:
- Load "books.xml" into xmlDoc using loadXMLDoc()
- Get the text node of the first <title> element
- Set the txt variable to the value of the text node
Example 2: Change the value of the element
The following code changes the value of the text node of the first <title> element:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue="Easy Cooking";
Code explanation:
- Load "books.xml" into xmlDoc using loadXMLDoc()
- Get the text node of the first <title> element
- Change the value of the text node to "Easy Cooking"
nodeType attribute
The nodeType attribute specifies the type of the node.
nodeType is read-only.
The most important node type is:
Element type | Node type |
---|---|
Elements | 1 |
Attributes | 2 |
Text | 3 |
Comments | 8 |
Document | 9 |
- Previous page DOM access node
- Next page DOM node list