Pagsasaklaw ng Nodo ng XML DOM
- Nakaraang Pahina Atribute at Method ng DOM
- Susunod na Pahina Impormasyon ng DOM Node
តាម DOM អ្នកអាចចូលទៅកែវនៅក្នុងឯកសារ XML។
Example
ឧទាហរណ៍ក្រោមនេះ ត្រូវបានប្រើរបស់របស់ XML books.xml។
មុខងារ loadXMLDoc(),ស្ថិតនៅ JavaScript ខាងក្រៅ ដើម្បីបង្កើតរបស់ XML។
- ចូលទៅកែវដោយប្រើសញ្ញារបស់បញ្ជីកែវ
- នៅក្នុងឧទាហរណ៍នេះ ត្រូវបានប្រើមធ្យម getElementsByTagname() ដើម្បីទទួលបានមុខដែក <title> ទីបីនៅ "books.xml" នោះ។
- ត្រួតពិនិត្យមុខដែកដោយប្រើមធ្យម length
- នៅក្នុងឧទាហរណ៍នេះ ត្រូវបានប្រើមធ្យម length ដើម្បីត្រួតពិនិត្យមុខដែក <title> ទាំងអស់នៅ "books.xml" នោះ។
- មើលប្រភេទកែវតួនាទី
- នៅក្នុងឧទាហរណ៍នេះ ត្រូវបានប្រើមធ្យម nodeType ដើម្បីទទួលបានប្រភេទកែវនៃមុខដែកដែលជាមុខដំបូងនៅ "books.xml" នោះ។
- ត្រួតពិនិត្យកែវតួនាទី
- នៅក្នុងឧទាហរណ៍នេះ ត្រូវបានប្រើមធ្យម nodeType ដើម្បីដោះស្រាយកែវតួនាទីនៅ "books.xml" នោះ។
- ត្រួតពិនិត្យកែវតួនាទីដោយប្រើទំនាក់ទំនងរវាងកែវ
- នៅក្នុងឧទាហរណ៍នេះ ត្រូវបានប្រើមធ្យម nodeType និង nextSibling ដើម្បីដោះស្រាយកែវតួនាទីនៅ "books.xml" នោះ។
ចូលទៅកែវ
អ្នកអាចចូលទៅកែវដោយបីវិធី:
- ដោយប្រើមធ្យម getElementsByTagName()
- ដោយប្រើការប្រើរបស់ការជំរុះ(ត្រួតពិនិត្យ)ឈើកែវ
- ដោយប្រើទំនាក់ទំនងរវាងកែវដើម្បីត្រូវតែងត្រានៅដើមឈើកែវ
getElementsByTagName() ម៉ូតវ៉ត
getElementsByTagName() ត្រូវអោយបានដែលមានផ្នែកស្លាកដែលបានបញ្ជាក់។
语法
node.getElementsByTagName("tagname");
Example
The following example returns all <title> elements under the x element:
x.getElementsByTagName("title");
Note that the example above only returns the <title> elements under the x node. To return all <title> elements in the XML document, use:
xmlDoc.getElementsByTagName("title");
Here, xmlDoc is the document itself (document node).
DOM Node List
getElementsByTagName() method returns a node list (node list). A node list is an array of nodes.
The following code uses loadXMLDoc() I-set ang "books.xml"Load xmlDoc into, then store a list of <title> nodes in the variable x:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title");
You can access the <title> element in x by index. To access the third <title>, you can write:
y=x[2];
Note:The index starts at 0.
In later chapters of this tutorial, you will learn more about Node List.
DOM Node List Length
The length property defines the length of the node list (i.e., the number of nodes).
You can loop through a node list by using the length property:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title"); for (i=0;i<x.length;i++) { document.write(x[i].childNodes[0].nodeValue); document.write("<br />"); }
Example explanation:
- Use loadXMLDoc() I-set ang "books.xml"Load xmlDoc
- Get all <title> element nodes
- Output the value of the text node of each <title> element
Node Type
XML document's documentElement Attributes are the root node.
Attributes of the node nodeName Attributes are the name of the node.
Attributes of the node nodeType Attributes are the type of the node.
You will learn more about node attributes in the next section of this tutorial.
Traverse the node
The following code loops through the child nodes of the root node, which are also element nodes:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.documentElement.childNodes; for (i=0;i<x.length;i++) { if (x[i].nodeType==1) {//Process only element nodes (type 1) document.write(x[i].nodeName); document.write("<br />"); } }
Example explanation:
- Sa pamamagitan ng paggamit ng loadXMLDoc() I-set ang "books.xml"I-load sa xmlDoc
- Get the child nodes of the root element
- Check the node type of each child node. If the node type is "1", it is an element node
- Kung ito ay element na node, i-output ang pangalan ng node
Gumagamit ng relasyon ng mga node sa paglalakbay
Ang mga sumusunod na code ay gumagamit ng relasyon ng mga node sa paglalakbay sa puno ng node:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book")[0].childNodes; y=xmlDoc.getElementsByTagName("book")[0].firstChild; for (i=0;i<x.length;i++) { kung (y.nodeType==1) {//Process only element nodes (type 1) document.write(y.nodeName + "<br />"); } y=y.nextSibling; }
- Sa pamamagitan ng paggamit ng loadXMLDoc() I-set ang "books.xml"I-load sa xmlDoc
- Makakuha ng unang anak ng unang element na may pangalan na "book"
- I-set ang "y" variable sa unang anak ng unang element na may pangalan na "book"
- Suriin ang lahat ng uri ng mga anak na node, kung ang uri ng node ay "1", ito ay element na node
- Kung ito ay element na node, i-output ang pangalan ng node
- I-set ang "y" variable sa susunod na同级 na node, at magsagawa muli ng loop
- Nakaraang Pahina Atribute at Method ng DOM
- Susunod na Pahina Impormasyon ng DOM Node