Linux技巧


导读 XML DOM 把 XML 文档视为一种树结构。这种树结构被称为节点树。可通过这棵树访问所有节点。可以修改或删除它们的内容,也可以创建新的元素。

创建新的元素节点

createElement() 方法创建一个新的元素节点:

实例

xmlDoc=loadXMLDoc(“books.xml”);
newel=xmlDoc.createElement(“edition”);
x=xmlDoc.getElementsByTagName(“book”)[0];
x.appendChild(newel);

实例解释:

使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
创建一个新的元素节点
向第一个 元素追加这个元素节点
创建新的属性节点

createAttribute() 用于创建一个新的属性节点:

实例

xmlDoc=loadXMLDoc(“books.xml”);
newatt=xmlDoc.createAttribute(“edition”);
newatt.nodeValue=”first”;
x=xmlDoc.getElementsByTagName(“title”);
x[0].setAttributeNode(newatt);

实例解释:

使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中
创建一个新的属性节点 “edition”
设置属性节点的值为 “first”
向第一个 元素添加这个新的属性节点</p> <p>注意:如果该属性已存在,则被新属性替代。</p> <p>使用 setAttribute() 创建属性</p> <p>由于 setAttribute() 方法可以在属性不存在的情况下创建新的属性,我们可以使用这个方法来创建一个新的属性。</p> <p>实例</p> <p>xmlDoc=loadXMLDoc(“books.xml”);<br /> x=xmlDoc.getElementsByTagName(‘book’);<br /> x[0].setAttribute(“edition”,”first”);</p> <p>实例解释:</p> <p>使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中<br /> 为第一个 <book> 元素设置(创建)值为 “first” 的 “edition” 属性<br /> 创建文本节点</p> <p>createTextNode() 方法创建一个新的文本节点:</p> <p>实例</p> <p>xmlDoc=loadXMLDoc(“books.xml”);<br /> newel=xmlDoc.createElement(“edition”);<br /> newtext=xmlDoc.createTextNode(“first”);<br /> newel.appendChild(newtext);<br /> x=xmlDoc.getElementsByTagName(“book”)[0];<br /> x.appendChild(newel);</p> <p>实例解释:</p> <p>使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中<br /> 创建一个新元素节点 <edition><br /> 创建一个新的文本节点,其文本是 “first”<br /> 向这个元素节点追加新的文本节点<br /> 向第一个 <book> 元素追加新的元素节点<br /> 创建 CDATA Section 节点</p> <p>createCDATASection() 方法创建一个新的 CDATA section 节点。</p> <p>实例</p> <p>xmlDoc=loadXMLDoc(“books.xml”);<br /> newCDATA=xmlDoc.createCDATASection(“Special Offer & Book Sale”);<br /> x=xmlDoc.getElementsByTagName(“book”)[0];<br /> x.appendChild(newCDATA);</p> <p>实例解释:</p> <p>使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中<br /> 创建一个新的 CDATA section 节点<br /> 向第一个 <book> 元素追加这个新的 CDATA section 节点<br /> 创建注释节点</p> <p>createComment() 方法创建一个新的注释节点。</p> <p>实例</p> <p>xmlDoc=loadXMLDoc(“books.xml”);<br /> newComment=xmlDoc.createComment(“Revised March 2008”);<br /> x=xmlDoc.getElementsByTagName(“book”)[0];<br /> x.appendChild(newComment);</p> <p>实例解释:</p> <p>使用 loadXMLDoc() 把 “books.xml” 载入 xmlDoc 中<br /> 创建一个新的注释节点<br /> 把这个新的注释节点追加到第一个 <book> 元素</p> <p>原文来自:https://www.runoob.com/dom/dom-nodes-create.html</p> <p>本文地址:https://www.linuxprobe.com/creation-node-overview.html<br /> 编辑:王艳敏,审核员:逄增宝</p> <p>Linux命令大全:https://www.linuxcool.com/</p> <p>Linux系统大全:https://www.linuxdown.com/</p> <p>红帽认证RHCE考试心得:https://www.rhce.net/</p> <p>为您推荐一些与本文相关的文章:<br /> 《图解网络硬件》pdf电子书免费下载<br /> Flatpak 1.1.0发布:可终止运行Flatpak实例<br /> 微软推出Windows 10 20H1 Build 19033快速测试版修复已知问题<br /> 小白也会安装pfSense-2.4.4<br /> Ubuntu 17.10 “Artful Aardvark” 发布首个 Beta<br /> Linux与设备信息的集合<br /> 曝趋势杀毒远程执行漏洞:可盗取用户所有密码<br /> 六种方法确保企业云计算成本<br /> 细述kubernetes开发流程<br /> 在Linux中以非root用户运行Docker技巧</p> </div><div class="post-meta wf-mobile-collapsed"><div class="entry-meta"><span class="category-link">Category: <a href="https://www.startupplaza.com/linux/" >Linux技巧</a></span><a class="author vcard" href="https://www.startupplaza.com/author/startupplaza/" title="查看startupplaza的所有文章" rel="author"><span class="fn">startupplaza</span></a><a href="https://www.startupplaza.com/2022/05/26/" title="下午2:31" class="data-link" rel="bookmark"><time class="entry-date updated" datetime="2022-05-26T14:31:14+08:00">2022年5月26日</time></a><a href="https://www.startupplaza.com/xml-dom-%e5%88%9b%e5%bb%ba%e8%8a%82%e7%82%b9%e6%a6%82%e8%bf%b0/#respond" class="comment-link" >评论</a></div></div> <div class="author-info entry-author"> <span class="author-avatar no-avatar"></span> <div class="author-description"> <h4><span class="author-heading">作者:</span> startupplaza</h4> <p class="author-bio"></p> </div> </div> <nav class="navigation post-navigation" role="navigation"><h2 class="screen-reader-text">文章导航</h2><div class="nav-links"><a class="nav-previous" href="https://www.startupplaza.com/asp-net-web-forms-hashtable-%e5%af%b9%e8%b1%a1%e7%ae%80%e4%bb%8b/" rel="prev"><i class="icomoon-the7-font-the7-arrow-29-3" aria-hidden="true"></i><span class="meta-nav" aria-hidden="true">历史的文章</span><span class="screen-reader-text">历史的文章:</span><span class="post-title h4-size">ASP.NET Web Forms – Hashtable 对象简介</span></a><a class="nav-next" href="https://www.startupplaza.com/python%e8%bf%9b%e5%ba%a6%e6%9d%a1%e6%8a%80%e5%b7%a7/" rel="next"><i class="icomoon-the7-font-the7-arrow-29-2" aria-hidden="true"></i><span class="meta-nav" aria-hidden="true">未来的文章</span><span class="screen-reader-text">未来的文章:</span><span class="post-title h4-size">Python进度条技巧</span></a></div></nav><div class="single-related-posts"><h3>Related Posts</h3><section class="items-grid"><div class=" related-item"><article class="post-format-standard"><div class="mini-post-img"><a class="alignleft post-rollover layzr-bg" href="https://www.startupplaza.com/%e5%8d%95%e7%82%b9%e7%99%bb%e5%bd%95%e4%b9%8bcas%e9%9b%86%e6%88%90sonar%e7%9a%84%e9%85%8d%e7%bd%ae%e6%96%b9%e6%b3%95%e8%af%a6%e8%a7%a3/" aria-label="Post image"><img class="preload-me lazy-load" src="data:image/svg+xml,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%20110%2080'%2F%3E" data-src="https://www.startupplaza.com/wp-content/uploads/2022/05/threat-trend-forecast-for-2019-110x80.jpg" data-srcset="https://www.startupplaza.com/wp-content/uploads/2022/05/threat-trend-forecast-for-2019-110x80.jpg 110w, https://www.startupplaza.com/wp-content/uploads/2022/05/threat-trend-forecast-for-2019-206x150.jpg 206w" width="110" height="80" alt="" /></a></div><div class="post-content"><a href="https://www.startupplaza.com/%e5%8d%95%e7%82%b9%e7%99%bb%e5%bd%95%e4%b9%8bcas%e9%9b%86%e6%88%90sonar%e7%9a%84%e9%85%8d%e7%bd%ae%e6%96%b9%e6%b3%95%e8%af%a6%e8%a7%a3/">单点登录之cas集成sonar的配置方法详解</a><br /><time class="text-secondary" datetime="2022-05-26T14:32:06+08:00">2022年5月26日</time></div></article></div><div class=" related-item"><article class="post-format-standard"><div class="mini-post-img"><a class="alignleft post-rollover layzr-bg" href="https://www.startupplaza.com/%e7%ae%80%e5%8d%95%e4%bb%8b%e7%bb%8drust%e4%b8%ad%e7%9a%84workspace/" aria-label="Post image"><img class="preload-me lazy-load" src="data:image/svg+xml,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%20110%2080'%2F%3E" data-src="https://www.startupplaza.com/wp-content/uploads/2022/05/security-zero-trust-network-1-110x80.jpg" data-srcset="https://www.startupplaza.com/wp-content/uploads/2022/05/security-zero-trust-network-1-110x80.jpg 110w, https://www.startupplaza.com/wp-content/uploads/2022/05/security-zero-trust-network-1-206x150.jpg 206w" width="110" height="80" alt="" /></a></div><div class="post-content"><a href="https://www.startupplaza.com/%e7%ae%80%e5%8d%95%e4%bb%8b%e7%bb%8drust%e4%b8%ad%e7%9a%84workspace/">简单介绍Rust中的workspace</a><br /><time class="text-secondary" datetime="2022-05-26T14:32:06+08:00">2022年5月26日</time></div></article></div><div class=" related-item"><article class="post-format-standard"><div class="mini-post-img"><a class="alignleft post-rollover layzr-bg" href="https://www.startupplaza.com/centos8-%e4%bd%bf%e7%94%a8realmd%e5%8a%a0%e5%85%a5ad%e5%9f%9f/" aria-label="Post image"><img class="preload-me lazy-load" src="data:image/svg+xml,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%20110%2080'%2F%3E" data-src="https://www.startupplaza.com/wp-content/uploads/2022/05/centos_pbis_domain_logo-1-110x80.jpg" data-srcset="https://www.startupplaza.com/wp-content/uploads/2022/05/centos_pbis_domain_logo-1-110x80.jpg 110w, https://www.startupplaza.com/wp-content/uploads/2022/05/centos_pbis_domain_logo-1-206x150.jpg 206w" width="110" height="80" alt="" /></a></div><div class="post-content"><a href="https://www.startupplaza.com/centos8-%e4%bd%bf%e7%94%a8realmd%e5%8a%a0%e5%85%a5ad%e5%9f%9f/">Centos8 使用realmd加入AD域</a><br /><time class="text-secondary" datetime="2022-05-26T14:32:06+08:00">2022年5月26日</time></div></article></div><div class=" related-item"><article class="post-format-standard"><div class="mini-post-img"><a class="alignleft post-rollover layzr-bg" href="https://www.startupplaza.com/%e7%ae%80%e5%8d%95%e6%8e%a2%e8%ae%a8golang%e4%b8%addefer%e9%a2%84%e8%ae%a1%e7%ae%97%e5%8f%82%e6%95%b0/" aria-label="Post image"><img class="preload-me lazy-load" src="data:image/svg+xml,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%20110%2080'%2F%3E" data-src="https://www.startupplaza.com/wp-content/uploads/2022/05/data-security-governanc-110x80.jpg" data-srcset="https://www.startupplaza.com/wp-content/uploads/2022/05/data-security-governanc-110x80.jpg 110w, https://www.startupplaza.com/wp-content/uploads/2022/05/data-security-governanc-206x150.jpg 206w" width="110" height="80" alt="" /></a></div><div class="post-content"><a href="https://www.startupplaza.com/%e7%ae%80%e5%8d%95%e6%8e%a2%e8%ae%a8golang%e4%b8%addefer%e9%a2%84%e8%ae%a1%e7%ae%97%e5%8f%82%e6%95%b0/">简单探讨Golang中defer预计算参数</a><br /><time class="text-secondary" datetime="2022-05-26T14:32:06+08:00">2022年5月26日</time></div></article></div><div class=" related-item"><article class="post-format-standard"><div class="mini-post-img"><a class="alignleft post-rollover layzr-bg" href="https://www.startupplaza.com/%e7%ae%80%e5%8d%95%e4%bb%8b%e7%bb%8dpython%e6%a0%bc%e5%bc%8f%e5%8c%96%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e6%a1%88%e4%be%8b%e6%96%b9%e6%b3%95/" aria-label="Post image"><img class="preload-me lazy-load" src="data:image/svg+xml,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%20110%2080'%2F%3E" data-src="https://www.startupplaza.com/wp-content/uploads/2022/05/python-f-strings-formatting-110x80.png" data-srcset="https://www.startupplaza.com/wp-content/uploads/2022/05/python-f-strings-formatting-110x80.png 110w, https://www.startupplaza.com/wp-content/uploads/2022/05/python-f-strings-formatting-206x150.png 206w" width="110" height="80" alt="" /></a></div><div class="post-content"><a href="https://www.startupplaza.com/%e7%ae%80%e5%8d%95%e4%bb%8b%e7%bb%8dpython%e6%a0%bc%e5%bc%8f%e5%8c%96%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e6%a1%88%e4%be%8b%e6%96%b9%e6%b3%95/">简单介绍Python格式化字符串的案例方法</a><br /><time class="text-secondary" datetime="2022-05-26T14:32:06+08:00">2022年5月26日</time></div></article></div><div class=" related-item"><article class="post-format-standard"><div class="mini-post-img"><a class="alignleft post-rollover layzr-bg" href="https://www.startupplaza.com/centos-8-%e4%b8%addnf%e7%ae%a1%e7%90%86%e5%99%a8%e5%a6%82%e4%bd%95%e4%bb%85%e4%b8%8b%e8%bd%bd%e4%b8%8d%e5%ae%89%e8%a3%85%e8%bd%af%e4%bb%b6/" aria-label="Post image"><img class="preload-me lazy-load" src="data:image/svg+xml,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%20110%2080'%2F%3E" data-src="https://www.startupplaza.com/wp-content/uploads/2022/05/centos8_dnf_logo-110x80.jpg" data-srcset="https://www.startupplaza.com/wp-content/uploads/2022/05/centos8_dnf_logo-110x80.jpg 110w, https://www.startupplaza.com/wp-content/uploads/2022/05/centos8_dnf_logo-206x150.jpg 206w" width="110" height="80" alt="" /></a></div><div class="post-content"><a href="https://www.startupplaza.com/centos-8-%e4%b8%addnf%e7%ae%a1%e7%90%86%e5%99%a8%e5%a6%82%e4%bd%95%e4%bb%85%e4%b8%8b%e8%bd%bd%e4%b8%8d%e5%ae%89%e8%a3%85%e8%bd%af%e4%bb%b6/">CentOS 8 中dnf管理器如何仅下载不安装软件</a><br /><time class="text-secondary" datetime="2022-05-26T14:32:06+08:00">2022年5月26日</time></div></article></div></section></div> </article> </div><!-- #content --> </div><!-- .wf-container --> </div><!-- .wf-wrap --> </div><!-- #main --> <!-- !Footer --> <footer id="footer" class="footer solid-bg"> <!-- !Bottom-bar --> <div id="bottom-bar" class="solid-bg logo-left" role="contentinfo"> <div class="wf-wrap"> <div class="wf-container-bottom"> <div id="branding-bottom"><a class="" href="https://www.startupplaza.com/"><img class=" preload-me" src="https://www.startupplaza.com/wp-content/uploads/2020/05/logo-white-2.png" srcset="https://www.startupplaza.com/wp-content/uploads/2020/05/logo-white-2.png 76w, https://www.startupplaza.com/wp-content/uploads/2020/05/logo-white-2.png 76w" width="76" height="22" sizes="76px" alt="上海创厦网络科技" /></a></div> <div class="wf-float-left"> 2015 - 2021 上海创厦网络科技有限公司 <a href=https://beian.miit.gov.cn/>沪ICP备15057283号-2</a> </div> <div class="wf-float-right"> <div class="bottom-text-block"><p>值班手机: 13801992572</p> </div> </div> </div><!-- .wf-container-bottom --> </div><!-- .wf-wrap --> </div><!-- #bottom-bar --> </footer><!-- #footer --> <a href="#" class="scroll-top"><span class="screen-reader-text">Go to Top</span></a> </div><!-- #page --> <style type="text/css"></style><script type='text/javascript' src='https://www.startupplaza.com/wp-content/themes/dt-the7/js/main.min.js?ver=8.6.0' id='dt-main-js'></script> <script type='text/javascript' src='https://www.startupplaza.com/wp-content/plugins/tf-numbers-number-counter-animaton/assets/js/tf_numbers.js?ver=1.8.1' id='tf_numbers-js'></script> <script type='text/javascript' src='https://www.startupplaza.com/wp-includes/js/comment-reply.min.js?ver=6.0.3' id='comment-reply-js'></script> <script type='text/javascript' src='https://www.startupplaza.com/wp-includes/js/hoverIntent.min.js?ver=1.10.2' id='hoverIntent-js'></script> <script type='text/javascript' id='megamenu-js-extra'> /* <![CDATA[ */ var megamenu = {"timeout":"300","interval":"100"}; /* ]]> */ </script> <script type='text/javascript' src='https://www.startupplaza.com/wp-content/plugins/megamenu/js/maxmegamenu.js?ver=2.9.8' id='megamenu-js'></script> <div class="pswp" tabindex="-1" role="dialog" aria-hidden="true"> <div class="pswp__bg"></div> <div class="pswp__scroll-wrap"> <div class="pswp__container"> <div class="pswp__item"></div> <div class="pswp__item"></div> <div class="pswp__item"></div> </div> <div class="pswp__ui pswp__ui--hidden"> <div class="pswp__top-bar"> <div class="pswp__counter"></div> <button class="pswp__button pswp__button--close" title="Close (Esc)"></button> <button class="pswp__button pswp__button--share" title="Share"></button> <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button> <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button> <div class="pswp__preloader"> <div class="pswp__preloader__icn"> <div class="pswp__preloader__cut"> <div class="pswp__preloader__donut"></div> </div> </div> </div> </div> <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap"> <div class="pswp__share-tooltip"></div> </div> <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)"> </button> <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)"> </button> <div class="pswp__caption"> <div class="pswp__caption__center"></div> </div> </div> </div> </div> </body> </html>