XML DOM进阶:值、属性、创建与删除
完整对应读取文本/属性、修改文本、全量加属性、创建和移除的六类操作。
学完这一章,你能够
- 完整对应读取文本/属性、修改文本、全量加属性、创建和移除的六类操作。
- 完整对应读取文本/属性、修改文本、全量加属性、创建和移除的六类操作。
读取与修改
本例读第一本书的title文本及lang属性,再修改文本;为每本书增加edition属性,并核对全部三本。getAttribute可能返回null,元素不在时应明确失败或降级。
元素nodeValue通常为null,文本节点才有实际nodeValue;textContent会替换后代内容,修改混合内容需清楚目标节点。集合动态变化时避免索引跳过。
创建和精确删除
createElement或createElementNS创建元素,createTextNode创建文本,appendChild加入指定父节点。本例创建edition子元素并追加到第一本书,序列化后重新解析核对。
removeChild(childNodes[0])可能删缩进文本而非业务元素;要删除title就先定位title或firstElementChild,检查父子关系。空白处理依解析器配置,不能简单归因某品牌永远忽略换行。
语法、用途与有效条件
| 项目 | 语法/输入 | 含义与结果 | 条件及限制 |
| 取元素值 | title.textContent/文本nodeValue | HTML与语义 | 区分元素nodeValue=null |
| 取属性 | getAttribute("lang") | zh | 缺少可null |
| 改文本 | firstChild.nodeValue | HTML现代语义 | 确认目标确实文本节点 |
| 加属性 | 每个book.setAttribute | 3本edition=first | 动态集合不删节点 |
| 建元素 | createElement+createTextNode+appendChild | edition=初版 | 对应父节点 |
| 删除 | removeChild(author节点) | 只删author | 不要盲删childNodes[0]空白 |
| 序列化 | XMLSerializer→DOMParser | 修改保留 | 不是原始字节等同 |
本章独立核心源码与验证步骤
展开查看收起
javascript
import {check,parse,textFile,xmlFile,put,done,text,tableCD,xhr} from './xml-browser.js';
try{
const doc=await xmlFile('books.xml'),books=doc.getElementsByTagName('book'),first=books[0],title=first.getElementsByTagName('title')[0],initial=title.textContent,lang=title.getAttribute('lang');check(initial==='HTML与语义'&&lang==='zh'&&title.nodeValue===null,'read text and attribute');title.firstChild.nodeValue='HTML现代语义';check(title.textContent==='HTML现代语义','text node change');for(const book of books)book.setAttribute('edition','first');check([...books].every(b=>b.getAttribute('edition')==='first'),'all 3 attributes');const edition=doc.createElement('edition'),value=doc.createTextNode('初版');edition.appendChild(value);first.appendChild(edition);check(edition.parentNode===first&&edition.textContent==='初版','create and append');check(doc.documentElement.firstChild.nodeType===Node.TEXT_NODE,'indentation is text');const removable=first.getElementsByTagName('author')[0];first.removeChild(removable);check(removable.parentNode===null&&!first.getElementsByTagName('author').length&&first.getElementsByTagName('title').length===1,'remove intended element');const serialized=new XMLSerializer().serializeToString(doc),again=parse(serialized);check(again.getElementsByTagName('edition')[0].textContent==='初版','roundtrip modifications');const actual={initial_title:initial,lang,changed_title:title.textContent,all_edition_attributes:3,new_child:edition.textContent,removed_author_not_whitespace:true,element_nodeValue:null,roundtrip:true};put('#operations',JSON.stringify(actual,null,2));done(actual);
}catch(error){window.mbskyError=String(error);document.querySelector('#status').textContent='运行失败:'+String(error)}
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 读标题/lang、改文本、3本加属性、建edition初版、只删author、往返后修改仍在。
本章附件与实例包
课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202611a539.zip
常见错误与边界检查
- 把良构、DTD、XSD、业务正确性或浏览器显示混为同一验收。
- CDATA被丢弃、默认命名空间传给属性、普通id自动是ID、空标签自带HTML行为等误解。
- 把服务器现代替代成功冒称ASP/IIS/旧浏览器或来源脚本已执行。
技术规范与官方资料
练习与参考解答
查看参考解答
用匹配的解析器和模型逐层验证,保留真实错误;输出文本由DOM/序列化器转义,文件/附件与章节绑定。