npm install xmlparsing> XML 解析器,支持布尔属性、自结束标签元素、保持元素属性。
``shell`
npm i xmlparsing
`js
import { parser, generator } from 'xmlparsing';
// parse
const xmlDocument = parser.parse('
// firstChild
const helloElement = xmlDocument.firstChild;
// tagName
console.info(helloElement.tagName); // hello
// getAttribute
console.info(helloElement.getAttribute('class')); // red;
helloElement.setAttribute('class', 'green');
// generate
generator.generate(xmlDocument); //
`
- Document
- Element
- Fragment
- Text
- Comment
- Cdata
类型:string
节点类型:document、element、fragment、text、comment、cdata 类型
类型:string | null
节点值:Text 节点、Comment 节点、Cdata 节点的文本内容,其他节点的 nodeValue 为 null
所有子节点数组
父节点
前一个兄弟节点
前一个兄弟元素节点
后一个兄弟节点
后一个兄弟元素节点
第一个子节点
最后一个子节点
节点字符串文本
所有子节点的字符串文本
说明:获取属性值
参数:attributeName 属性名
`js`
//
node.getAttribute('class'); // 'hello'
说明: 设置属性值
参数: attributeName 属性名, newValue 属性值
`js`
//
node.getAttribute('class', 'green');
node.getAttribute('id', 'abc'); //
说明: 删除节点属性
参数: attributeName
`js`
//
node.removeAttribute('class');
node.getAttribute('id', 'abc'); //
说明: 判断节点是否有纯在的属性
参数: attributeName
`js`
//
node.removeAttribute('class');
node.getAttribute('id', 'abc'); //
说明: 添加子节点
参数: childNode
`js`
//
const newNode = node.createElement('xyz');
node.appendChild(newNode); //
说明: 在参考节点前插入节点
参数: newNode 新节点,referenceNode 参考相关节点
`js`
//
const newNode = node.createElement('y');
node.insertBefore(newNode, node.firstChild); //
说明: 在参考节点后插入节点
参数: newNode 新节点,referenceNode 参考相关节点
`js`
//
const newNode = node.createElement('y');
node.insertAfter(newNode, node.firstChild); //
说明:在节点前添加新节点
参数: newNode
`js`
//
const newNode = node.createElement('y');
node.before(newNode); //
说明:在节点后添加新节点
参数: newNode
`js`
//
const newNode = node.createElement('y');
node.after(newNode); //
说明:删除子节点
参数: newNode
`js``
//
const newNode = node.removeChild(node.firstChild);
说明:用新节点替换节点本身
说明:删除节点本身
说明:清空节点所有子节点
说明:克隆节点本身
说明:返回节点字符串序列
说明:返回所有指定节点名称的所有子节点
说明:创建新元素节点
说明:创建注释节点
说明:创建文本节点
说明:创建片段节点