XML DOM - 导航

到目前为止,我们研究了DOM结构,如何加载和解析XML DOM对象以及遍历DOM对象.在这里,我们将看到如何在DOM对象中的节点之间导航. XML DOM包含节点的各种属性,可帮助我们浏览节点,例如 :

  • parentNode

  • childNodes

  • firstChild

  • lastChild

  • nextSibling

  • previousSibling

以下是显示其与其他节点的关系的节点树图.

XML DOM Navigation

DOM  - 父节点

此属性指定父节点作为节点对象.

示例

以下示例(navigate_example.htm)解析XML文档( node.xml )到XML DOM对象中.然后DOM对象通过子节点导航到父节点 :

<!DOCTYPE html>
<html>
   <body>
      <script>
         if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
         } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.open("GET","https://img01.yuandaxia.cn/Content/img/tutorials/dom/node.xml",false);
         xmlhttp.send();
         xmlDoc = xmlhttp.responseXML;

         var y = xmlDoc.getElementsByTagName("Employee")[0];
         document.write(y.parentNode.nodeName);
      </script>
   </body>
</html>

如上例所示,子节点 Employee 导航到其父节点.

执行

将此文件保存为服务器路径上的 navigate_example.html (此文件和node.xml 应位于服务器的同一路径上).在输出中,我们得到 Employee 的父节点,即 Company .

First Child

此属性的类型为 Node ,表示NodeList中存在的第一个子名称.

示例

以下示例(first_node_example.htm)将XML文档( node.xml )解析为XML DOM对象然后导航到DOM对象中的第一个子节点.

<!DOCTYPE html>
<html>
   <body>
      <script>
         if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
         } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.open("GET","https://img01.yuandaxia.cn/Content/img/tutorials/dom/node.xml",false);
         xmlhttp.send();
         xmlDoc = xmlhttp.responseXML;

         function get_firstChild(p) {
            a = p.firstChild;

            while (a.nodeType != 1) {
               a = a.nextSibling;
            }
            return a;
         }
         var firstchild = get_firstChild(xmlDoc.getElementsByTagName("Employee")[0]);
         document.write(firstchild.nodeName);
      </script>
   </body>
</html>

  • 函数 get_firstChild(p)用于避免空节点.它有助于从节点列表中获取firstChild元素.

  • x = get_firstChild(xmlDoc.getElementsByTagName("Employee")[0])获取标记名称 Employee 的第一个子节点.

执行

将此文件保存为服务器路径上的 first_node_example.htm (此文件和 node.xml 应位于服务器的同一路径上).在输出中,我们得到 Employee 的第一个子节点,即 FirstName .

最后一个孩子

此属性的类型为 Node ,表示NodeList中存在的最后一个子名称.

示例

以下示例(last_node_example.htm)将XML文档( node.xml )解析为XML DOM对象,然后导航到xml DOM对象中的最后一个子节点.

<!DOCTYPE html>
  <body>
      <script>
         if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
         } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.open("GET","https://img01.yuandaxia.cn/Content/img/tutorials/dom/node.xml",false);
         xmlhttp.send();
         xmlDoc = xmlhttp.responseXML;

         function get_lastChild(p) {
            a = p.lastChild;

            while (a.nodeType != 1){
               a = a.previousSibling;
            }
            return a;
         }
         var lastchild = get_lastChild(xmlDoc.getElementsByTagName("Employee")[0]);
         document.write(lastchild.nodeName);
      </script>
   </body>
</html>

执行

将此文件保存为服务器路径上的 last_node_example.htm (此file和node.xml应位于服务器的同一路径上).在输出中,我们得到 Employee的最后一个子节点,,即电子邮件.

Next Sibling

此属性的类型为 Node ,表示下一个子节点,即NodeList中指定的子元素的下一个兄弟.

示例

以下示例(nextSibling_example.htm)解析XML文档(节点. xml )转换为XML DOM对象,该对象立即导航到xml文档中的下一个节点.

<!DOCTYPE html>
   <body>
      <script>
         if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
         }
         else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.open("GET","https://img01.yuandaxia.cn/Content/img/tutorials/dom/node.xml",false);
         xmlhttp.send();
         xmlDoc = xmlhttp.responseXML;

         function get_nextSibling(p) {
            a = p.nextSibling;

            while (a.nodeType != 1) {
               a = a.nextSibling;
            }
            return a;
         }
         var nextsibling = get_nextSibling(xmlDoc.getElementsByTagName("FirstName")[0]);
         document.write(nextsibling.nodeName);
      </script>
   </body>
</html>

执行

将此文件保存为服务器路径上的 nextSibling_example.htm (此file和node.xml应位于服务器的同一路径上).在输出中,我们得到 FirstName的下一个兄弟节点,,即 LastName .

以前的兄弟姐妹

此属性的类型为 Node ,表示前一个子节点,即NodeList中指定的子元素的前一个兄弟节点.

示例

以下示例(previoussibling_example.htm)解析XML文档(节点. xml )进入XML DOM对象,然后导航xml文档中存在的最后一个子节点的before节点.

<!DOCTYPE html>
   <body>
      <script>
         if (window.XMLHttpRequest)
         {
            xmlhttp = new XMLHttpRequest();
         } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.open("GET","https://img01.yuandaxia.cn/Content/img/tutorials/dom/node.xml",false);
         xmlhttp.send();
         xmlDoc = xmlhttp.responseXML;

         function get_previousSibling(p) {
            a = p.previousSibling;

            while (a.nodeType != 1) {
               a = a.previousSibling;
            }
            return a;
         }

         prevsibling = get_previousSibling(xmlDoc.getElementsByTagName("Email")[0]);
         document.write(prevsibling.nodeName);
      </script>
   </body>
</html>

执行

将此文件保存为服务器路径上的 previoussibling_example.htm 文件和 node.xml 应位于服务器的同一路径上.在输出中,我们得到电子邮件的前一个兄弟节点,,即 ContactNo .