https://github.com/zhengsk/xmlparsing
An xml parser.
https://github.com/zhengsk/xmlparsing
ast generator parser xml
Last synced: about 1 year ago
JSON representation
An xml parser.
- Host: GitHub
- URL: https://github.com/zhengsk/xmlparsing
- Owner: zhengsk
- Created: 2018-08-17T12:26:39.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-02-02T15:20:07.000Z (over 6 years ago)
- Last Synced: 2025-05-20T02:22:58.216Z (about 1 year ago)
- Topics: ast, generator, parser, xml
- Language: TypeScript
- Homepage: https://zhengsk.github.io/xmlparsing/
- Size: 654 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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
## 属性
### nodeType
类型:string
节点类型:document、element、fragment、text、comment、cdata 类型
### nodeValue
类型:string | null
节点值:Text 节点、Comment 节点、Cdata 节点的文本内容,其他节点的 nodeValue 为 null
### children [Node]
所有子节点数组
### parentNode
父节点
### previousSibling
前一个兄弟节点
### previousElementSibling
前一个兄弟元素节点
### nextSibling
后一个兄弟节点
### nextElementSibling
后一个兄弟元素节点
### firstChild
第一个子节点
### lastChild
最后一个子节点
### outerXML
节点字符串文本
### innerXML
所有子节点的字符串文本
## 方法
### getAttribute(attributName)
说明:获取属性值
参数:attributeName 属性名
```js
//
node.getAttribute('class'); // 'hello'
```
### setAttribute(attributeName, newValue)
说明: 设置属性值
参数: attributeName 属性名, newValue 属性值
```js
//
node.getAttribute('class', 'green');
node.getAttribute('id', 'abc'); //
```
### removeAttribute(attributeName)
说明: 删除节点属性
参数: attributeName
```js
//
node.removeAttribute('class');
node.getAttribute('id', 'abc'); //
```
### hasAttribute(attributName)
说明: 判断节点是否有纯在的属性
参数: attributeName
```js
//
node.removeAttribute('class');
node.getAttribute('id', 'abc'); //
```
### appendChild(childNode)
说明: 添加子节点
参数: childNode
```js
//
const newNode = node.createElement('xyz');
node.appendChild(newNode); //
```
### insertBefore(newNode, referenceNode)
说明: 在参考节点前插入节点
参数: newNode 新节点,referenceNode 参考相关节点
```js
//
const newNode = node.createElement('y');
node.insertBefore(newNode, node.firstChild); //
```
### insertAfter(newNode, referenceNode)
说明: 在参考节点后插入节点
参数: newNode 新节点,referenceNode 参考相关节点
```js
//
const newNode = node.createElement('y');
node.insertAfter(newNode, node.firstChild); //
```
### before(newNode[, newNode...])
说明:在节点前添加新节点
参数: newNode
```js
//
const newNode = node.createElement('y');
node.before(newNode); //
```
### after(newNode[, newNode...])
说明:在节点后添加新节点
参数: newNode
```js
//
const newNode = node.createElement('y');
node.after(newNode); //
```
### removeChild(childNode)
说明:删除子节点
参数: newNode
```js
//
const newNode = node.removeChild(node.firstChild);
```
### replaceWith(newNode)
说明:用新节点替换节点本身
### remove()
说明:删除节点本身
### empty()
说明:清空节点所有子节点
### cloneNode([deep])
说明:克隆节点本身
### toString()
说明:返回节点字符串序列
### getElementsByTagName(tagName)
说明:返回所有指定节点名称的所有子节点
### createElement(nodeType)
说明:创建新元素节点
### createComment([commentText])
说明:创建注释节点
### createTextNode([text])
说明:创建文本节点
### createFragment()
说明:创建片段节点