教程 / XML / Web Service / XML
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)}
XML DOM进阶:值、属性、创建与删除:独立核心实例
所需环境:Windows / Python3.12+lxml6.1.3 / Edge156 / 自有本地HTTP,PHP为独立附例
当前源码 SHA256:dba5486eafade6bddd59fb69d61c64aebf648a31150ee9889723092973d6f412
预期结果 · 待实际运行核验
- 读标题/lang、改文本、3本加属性、建edition初版、只删author、往返后修改仍在。
常见错误与边界
- 把良构、DTD、XSD、业务正确性或浏览器显示混为同一验收。
- CDATA被丢弃、默认命名空间传给属性、普通id自动是ID、空标签自带HTML行为等误解。
- 把服务器现代替代成功冒称ASP/IIS/旧浏览器或来源脚本已执行。
练习与解答
只改一个本章受控输入,写明版本/编码/命名空间/预期有效性与观察,再运行对应程序。
展开参考解答
用匹配的解析器和模型逐层验证,保留真实错误;输出文本由DOM/序列化器转义,文件/附件与章节绑定。