访问节点
可以通过三种方式来访问节点:
1. 通过使用 getElementsByTagName() 方法。
2. 通过循环(遍历)节点树。
3. 通过利用节点的关系在节点树中导航。
getElementsByTagName() 方法
getElementsByTagName() 返回拥有指定标签名的所有元素。
语法
node.getElementsByTagName("tagname");
实例
下面的实例返回 x 元素下的所有
元素:</p><p>x.getElementsByTagName("title");</p><p>请注意,上面的实例仅返回 x 节点下的 <title> 元素。如需返回 XML 文档中的所有 <title> 元素,请使用:</p><p>xmlDoc.getElementsByTagName("title");</p><p>在这里,xmlDoc 就是文档本身(文档节点)。</p><p>DOM 节点列表(Node List)</p><p>getElementsByTagName() 方法返回节点列表。节点列表是节点的数组。</p><p>下面的代码使用 loadXMLDoc() 把 "books.xml" 载入 xmlDoc 中,然后在变量 x 中存储 <title> 节点的一个列表:</p><p>xmlDoc=loadXMLDoc("books.xml");</p><p>x=xmlDoc.getElementsByTagName("title");</p><p>可通过索引号访问 x 中的 <title> 元素。如需访问第三个 <title>,您可以编写:</p><p>y=x[2];</p><p>注意:该索引从 0 开始。</p><p>在本教程后面的章节中,您将学习更多有关节点列表(Node List)的知识。</p><p>DOM 节点列表长度(Node List Length)</p><p>length 属性定义节点列表的长度(即节点的数量)。</p><p>您可以通过使用 length 属性来<a target="_blank" href="https://www.huoban.com/news/tags-3590.html"style="font-weight:bold;">遍历</a>节点列表:</p><p>实例</p><p>xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title"); for (i=0;i<x.length;i++) { document.write(x[i].childNodes[0].nodeValue); document.write(""); }</p><p>实例解释:</p><p>使用 loadXMLDoc() 把 "books.xml" 载入 xmlDoc 中</p><p>获取所有 <title> 元素节点</p><p>输出每个 <title> 元素的文本节点的值</p><p>节点类型(Node Types)</p><p>XML 文档的 documentElement 属性是根节点。</p><p>节点的 nodeName 属性是节点的名称。</p><p>节点的 nodeType 属性是节点的类型。</p><p>您将在本教程的下一章中学习更多有关节点属性的知识。</p><p>尝试一下</p><p>遍历节点</p><p>下面的代码遍历根节点的子节点,同时也是元素节点:</p><p>实例</p><p>xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.documentElement.childNodes; for (i=0;i<x.length;i++) { if (x[i].nodeType==1) { // 执行一次 document.write(x[i].nodeName); document.write(""); } }</p><p>实例解释:</p><p>使用 loadXMLDoc() 把 "books.xml" 载入 xmlDoc 中</p><p>获取根元素的子节点</p><p>检查每个子节点的节点类型。如果节点类型是 "1",则是元素节点</p><p>如果是元素节点,则输出节点的名称</p><p>导航节点的关系</p><p>下面的代码使用节点关系导航节点树:</p><p>实例</p><p>xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book")[0].childNodes; y=xmlDoc.getElementsByTagName("book")[0].firstChild; for (i=0;i<x.length;i++) { if (y.nodeType==1) { // 输出节点名 document.write(y.nodeName + ""); } y=y.nextSibling; }</p><p style="text-align:center"><img src="https://www.huoban.com/news/zb_users/cache/ly_autoimg/m/MTA0NTc.jpg" alt="XML DOM - 访问节点" title="XML DOM - 访问节点" /></p><p>使用 loadXMLDoc() 把 "books.xml" 载入 xmlDoc 中</p><p>获取第一个 book 元素的子节点</p><p>把 "y" 变量设置为第一个 book 元素的第一个子节点</p><p>对于每个子节点(第一个子节点从 "y" 开始),检查节点类型,如果节点类型为 "1",则是元素节点</p><p>如果是元素节点,则输出该节点的名称</p><p>把 "y" 变量设置为下一个同级节点,并再次运行循环</p><p>Java XML</p><p>
<strong>版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。</strong>
</p><p>
<strong>版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。</strong>
</p></div>
<div class="article_footer clear">
<div class="fr tag">标签:<a href="https://www.huoban.com/news/tags-2749.html">Element</a>
<a href="https://www.huoban.com/news/tags-3590.html">遍历</a>
<a href="https://www.huoban.com/news/tags-520.html">方法</a>
</div>
<div class="bdsharebuttonbox fl share">
<div class="share-widget fl">
<div class="social-share" data-sites="wechat,weibo, qq, qzone"></div>
</div>
</div>
</div>
<!-- 广告位ad4 -->
<div class="post-navigation clear">
<div class="post-previous fl">
<span>上一篇:</span><a href="https://www.huoban.com/news/post/670.html">在 Linux 上<a target="_blank" href="https://www.huoban.com/news/tags-823.html"style="font-weight:bold;">设置</a>用户和组磁盘配额的 5 个步骤</a>
</div>
<div class="post-next fr">
<span>下一篇:</span><a href="https://www.huoban.com/news/post/77269.html">怎样使文本一点一点出来</a>
</div>
</div>
</div>
<div class="related_article">
<div class="box_title clear">
<span><i class="icon fa fa-paper-plane"></i>相关文章</span>
</div>
<div class="related_list clear">
<article class="fl">
<div class="related_img"><a href="https://www.huoban.com/news/post/13969.html"><img src="https://www.huoban.com/news/zb_users/cache/ly_autoimg/m/MTM5Njk.jpg"></a></div>
<div class="related_detail">
<h3><a href="https://www.huoban.com/news/post/13969.html" title="XML DOM 获取节点值">XML DOM 获取节点值</a></h3>
<div class="meta">
<span><i class="fa fa-eye"></i>592</span>
<span><i class="fa fa-clock-o"></i>2025-04-01</span>
</div>
</div>
</article>
<article class="fl">
<div class="related_img"><a href="https://www.huoban.com/news/post/7857.html"><img src="https://www.huoban.com/news/zb_users/upload/2023/08/wenzhangerweima.jpg"></a></div>
<div class="related_detail">
<h3><a href="https://www.huoban.com/news/post/7857.html" title="AcWing基础算法课Level-2 第三讲 搜索与图论">AcWing基础算法课Level-2 第三讲 搜索与图论</a></h3>
<div class="meta">
<span><i class="fa fa-eye"></i>592</span>
<span><i class="fa fa-clock-o"></i>2025-04-01</span>
</div>
</div>
</article>
<article class="fl">
<div class="related_img"><a href="https://www.huoban.com/news/post/10214.html"><img src="https://www.huoban.com/news/zb_users/upload/2022/05/20220529195608_94489.css('color','green"></a></div>
<div class="related_detail">
<h3><a href="https://www.huoban.com/news/post/10214.html" title="jQuery选择器">jQuery选择器</a></h3>
<div class="meta">
<span><i class="fa fa-eye"></i>592</span>
<span><i class="fa fa-clock-o"></i>2025-04-01</span>
</div>
</div>
</article>
</div>
</div>
<!--<p class="comment-disable sb br mb"><i class="iconfont icon-cry"></i>抱歉,评论功能暂时关闭!</p>-->
</div>
</div>
<div class="sidebar">
<div id="推荐文章" class="part clear 推荐文章">
<div class="top">
<h3 class="title">推荐文章</h3>
</div>
<div class="side 推荐文章"><ul><ul class="hot_posts"> <li><h4><a href="https://www.huoban.com/news/post/132763.html" title="企业生产管理是什么,企业生产管理软件">企业生产管理是什么,企业生产管理软件</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/136160.html" title="盘点进销存软件排行榜前十名">进盘点进销存软件排行榜前十名</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132779.html" title="进销存系统哪个简单好用?进销存系统优点">进销存系统哪个简单好用?进销存系统优点</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/133648.html" title="工厂生产管理(工厂生产管理流程及制度)">工厂生产管理(工厂生产管理流程及制度)</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132780.html" title="生产管理软件,机械制造业生产管理,制造业生产过程管理软件">生产管理软件,机械制造业生产管理,制造业生产过程管理软件</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132776.html" title="进销存软件和ERP有什么区别?进销存与erp软件理解">进销存软件和ERP有什么区别?进销存与erp软件理解</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132974.html" title="进销存如何进行库存管理">进销存如何进行库存管理</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132269.html" title="excel销售订单管理系统(销售订单录入系统)">如何利用excel制作销售订单管理系统?</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/136946.html" title="数据库订单管理系统有哪些功能?数据库订单管理系统怎么设计?">数据库订单管理系统有哪些功能?数据库订单管理系统怎么设计?</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/132312.html" title="数据库订单管理系统(订单系统数据流图)">什么是数据库管理系统?</a></h4></li></ul></ul></div>
</div>
<div id="divPrevious" class="part clear previous">
<div class="top">
<h3 class="title">最近发表</h3>
</div>
<div class="side divPrevious"><ul><li><a title="宠物集市-宠物集市华东、华南、华北排行榜一览表" href="https://www.huoban.com/news/post/157796.html">宠物集市-宠物集市华东、华南、华北排行榜一览表</a></li>
<li><a title="京宠展-2025年时间表" href="https://www.huoban.com/news/post/157795.html">京宠展-2025年时间表</a></li>
<li><a title="宠物集市在深圳哪里有?时间地址最新消息" href="https://www.huoban.com/news/post/157794.html">宠物集市在深圳哪里有?时间地址最新消息</a></li>
<li><a title="亚洲宠物展2025年展会介绍" href="https://www.huoban.com/news/post/157793.html">亚洲宠物展2025年展会介绍</a></li>
<li><a title="京宠展信息指南" href="https://www.huoban.com/news/post/157792.html">京宠展信息指南</a></li>
<li><a title="宠物展会2025年时间表" href="https://www.huoban.com/news/post/157791.html">宠物展会2025年时间表</a></li>
<li><a title="亚宠展、全球宠物产业风向标——亚洲宠物展览会深度解析" href="https://www.huoban.com/news/post/157790.html">亚宠展、全球宠物产业风向标——亚洲宠物展览会深度解析</a></li>
<li><a title="2025年亚洲宠物展览会、京宠展有哪些亮点" href="https://www.huoban.com/news/post/157789.html">2025年亚洲宠物展览会、京宠展有哪些亮点</a></li>
<li><a title="<a target="_blank" href="https://www.huoban.com/news/tags-10262.html"style="font-weight:bold;">wps</a>演示添加自定义按钮<a target="_blank" href="https://www.huoban.com/news/tags-823.html"style="font-weight:bold;">设置</a>动作改变按顺序播放" href="https://www.huoban.com/news/post/118206.html"><a target="_blank" href="https://www.huoban.com/news/tags-10262.html"style="font-weight:bold;">wps</a>演示添加自定义按钮<a target="_blank" href="https://www.huoban.com/news/tags-823.html"style="font-weight:bold;">设置</a>动作改变按顺序播放</a></li>
<li><a title="如何将<a target="_blank" href="https://www.huoban.com/news/tags-13007.html"style="font-weight:bold;">WPS</a>行中的文本调整到行中" href="https://www.huoban.com/news/post/119027.html">如何将<a target="_blank" href="https://www.huoban.com/news/tags-13007.html"style="font-weight:bold;">WPS</a>行中的文本调整到行中</a></li>
</ul></div>
</div>
<div id="sidebar_ad" class="part clear sidebar_ad">
<div class="part sidebar_ad"></div>
</div>
<div id="hot_posts" class="part clear hot_posts">
<div class="top">
<h3 class="title">热评文章</h3>
</div>
<ul class="hot_posts"><li><h4><a href="https://www.huoban.com/news/post/104011.html" title="<a target="_blank" href="https://www.huoban.com/news/tags-10179.html"style="font-weight:bold;">零代码</a>开发是什么?2022<a target="_blank" href="https://www.huoban.com/news/tags-1.html"style="font-weight:bold;">低代码</a>平台排行榜">零代码开发是什么?2022低代码平台排行榜</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/131019.html" title="智能<a target="_blank" href="https://www.huoban.com/news/tags-14329.html"style="font-weight:bold;">进销存库存管理</a><a target="_blank" href="https://www.huoban.com/news/tags-56.html"style="font-weight:bold;">系统</a>(智慧进销存)">智能进销存库存管理系统(智慧进销存)</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/73907.html" title="<a target="_blank" href="https://www.huoban.com/news/tags-14095.html"style="font-weight:bold;">在线文档</a>哪家强?8款在线文档编辑软件推荐">在线文档哪家强?8款在线文档编辑软件推荐</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/102663.html" title="WPS2016怎么绘制简单的价格表?">WPS2016怎么绘制简单的价格表?</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/151831.html" title="<a target="_blank" href="https://www.huoban.com/news/tags-14164.html"style="font-weight:bold;">客户管理工具</a>是什么?">客户管理工具是什么?</a></h4></li><li><h4><a href="https://www.huoban.com/news/post/145308.html" title="<a target="_blank" href="https://www.huoban.com/news/tags-11447.html"style="font-weight:bold;">Excel</a>项目进度表模板,简化您的项目进度<a target="_blank" href="https://www.huoban.com/news/tags-485.html"style="font-weight:bold;">管理</a>">Excel项目进度表模板,简化您的项目进度管理</a></h4></li></ul> </div>
<div id="divLinkage" class="part clear link">
<div class="top">
<h3 class="title">友情链接</h3>
</div>
<div class="side divLinkage"><ul><li class="link-item"><a href="https://www.huoban.com/" target="_blank" title="伙伴云">伙伴云</a></li><li class="link-item"><a href="https://www.huoban.com/news/category-19.html" title="进销存管理">进销存管理</a></li><li class="link-item"><a href="https://www.huoban.com/news/category-3.html" title="低代码">低代码</a></li><li class="link-item"><a href="https://www.huoban.com/news/tags-12.html" target="_blank" title="Excel表格">Excel表格</a></li><li class="link-item"><a href="http://www.taozhaigongsiw.com/" title="诚鼎网">诚鼎网</a></li><li class="link-item"><a href="https://www.finclip.com/news/" title="FinClip">FinClip</a></li><li class="link-item"><a href="http://www.qdhtbl.com" title="海特贝利常识网">海特贝利常识网</a></li></ul></div>
</div> </div>
</div>
</section>
</div>
<footer class="p-footer">
<div class="contant_box">
<div class="discover_tmt">
<h5 class="" style="font-size: 1px; color: white;">伙伴云</h5>
<div class="text_box">
<a href="https://www.jiasou.cn/article/" title="toB数字化营销SEO" style="font-size: 1px; color: white;">加搜toBSEO</a>
<a href="https://www.finclip.com/news/category-1.html" title="小程序工具" style="font-size: 1px; color: white;">前端框架</a>
<a href="https://www.jia-ai.com/info/" title="小红书营销攻略" style="font-size: 1px; color: white;">小红书营销攻略</a>
<a href="https://www.yanyin.tech/cms/" title="分子生物学知识" style="font-size: 1px; color: white;">分子生物学知识</a>
<a href="https://www.finclip.com/news/" title="FinClip 技术文档" style="font-size: 1px; color: white;">小程序容器帮助中心</a>
<a href="https://www.finclip.com/news/article/" title="小程序开发行业洞察" style="font-size: 1px; color: white;">小程序开发行业洞察</a>
<a href="https://www.foneplatform.com/jscms/" title="全面预算管理资讯" style="font-size: 1px; color: white;">全面预算管理资讯</a>
<a href="https://www.weiling.cn/article/" title="企微SCRM客户管理干货" style="font-size: 1px; color: white;">企微SCRM客户管理干货</a>
<a href="https://www.transfertech.cn/news/" title="3D视觉相机资讯" style="font-size: 1px; color: white;">3D视觉相机资讯</a>
<a href="https://www.i2cool.com.cn/" title="创冷科技无电制冷" style="font-size: 1px; color: #22292D;">创冷科技无电制冷</a>
<a href="https://www.elibot.com/tideflow/" title="协作机器人资讯" style="font-size: 1px; color: #22292D;">协作机器人资讯</a>
</div> </div>
<div class="collaboration_box">
</div>
<div class="we_img_box clear">
<div class="img_box">
<img src="https://www.huoban.com/news/zb_users/theme/zblog5_news/image/ewm.png" alt="" class="hover_tmt">
</div>
</div>
</div>
<p class="info"> <a href="https://beian.miit.gov.cn" target="_blank" rel="nofollow">京ICP备12038259号</a>
<span>
<a href="#"></a></span>
</p>
</footer>
<div id="backtop" class="backtop">
<div class="bt-box top">
<i class="fa fa-angle-up fa-2x"></i>
</div>
</div>
<script charset="UTF-8" src="https://www.huoban.com/assets/js/sensorsdata.1.22.2.min.js"></script>
<script charset="UTF-8">
var sensors = window['sensorsDataAnalytic201505'];
sensors.init({
server_url: 'https://saapi.huoban.com/sa?project=production',
heatmap:{scroll_notice_map:'not_collect'},
use_client_time:true,
send_type:'beacon'
});
sensors.quick('autoTrack');
</script>
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?6444c045836d6bf27124085a4f62c2a8";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<script>
(()=>{const e="https://analyze.jiasou.cc/api/v1/page_view/report/",n="9fe06d4884e0461caaa1de5651164d43";let t=null;const o=new Proxy({},{get:(e,n)=>localStorage.getItem(window.btoa(n)),set:(e,n,t)=>!!t&&(localStorage.setItem(window.btoa(n),t),!0)});new Promise((t=>{if(o.fingerprint)t();else{const a=function(){var e={};if(e.userAgent=navigator.userAgent||"",e.plugins=[],navigator.plugins&&navigator.plugins.length>0)for(var n=0;n<navigator.plugins.length;n++){var t={name:navigator.plugins[n].name||"",filename:navigator.plugins[n].filename||"",description:navigator.plugins[n].description||""};e.plugins.push(t)}e.languages=navigator.languages||[navigator.language||""],e.timezone=(new Date).getTimezoneOffset(),e.screenResolution={width:window.screen.width||0,height:window.screen.height||0,pixelDepth:window.screen.pixelDepth||0,colorDepth:window.screen.colorDepth||0};var o=document.createElement("canvas").getContext("2d"),a=[],i=["monospace","sans-serif","serif"];for(n=0;n<i.length;n++){var r=i[n];o.font="12px "+r,o.measureText("abcdefghijklmnopqrstuvwxyz0123456789").width>0&&a.push(r)}return e.fonts=a,e.cookieEnabled=navigator.cookieEnabled||!1,e.localStorage=void 0!==window.localStorage,e.sessionStorage=void 0!==window.sessionStorage,e.doNotTrack="1"===navigator.doNotTrack||"1"===window.doNotTrack||"1"===navigator.msDoNotTrack||"yes"===navigator.doNotTrack,e}();fetch(`${e}u/`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({key:n,f:window.btoa(JSON.stringify(a))})}).then((e=>{console.debug("browser fingerprint sent"),200===e.status&&e.json().then((e=>{console.debug("browser fingerprint received",e),o.fingerprint=e.fp,t()}))}))}})).then((()=>{e&&o.fingerprint&&fetch(e+`?${new URLSearchParams({token:n}).toString()}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({c:window.btoa(JSON.stringify({u:o.fingerprint,l:window.location.href,r:document.referrer}))})}).then((e=>{200==e.status&&e.json().then((e=>{e.track_id&&(t=e.track_id)}))}))})),window.addEventListener("beforeunload",(async n=>{t&&fetch(e+`?${new URLSearchParams({track_id:t}).toString()}`,{method:"GET",headers:{"Content-Type":"text/plain"},keepalive:!0}),n.returnValue=""}))})();
</script><script language="javascript" src="https://www.huoban.com/news/zb_users/plugin/ZF_ad/js/index.js?id=959"></script>
<script language="javascript" src="https://www.huoban.com/news/zb_users/plugin/ZF_ad/js/ZF_ad__cookie.js"></script>
</body>
</html>
<!--44.82 ms , 17 queries , 3705kb memory , 0 error-->