0.0.1 • Published 6 years ago
xml-js-parse v0.0.1
xml-js-parse
Parse the xml as a js object or build an xml through a js object.
Usage
npm i xml-js-parse -S
const xmlParser = new require('xml-js-parse').Parser()
const xml = `
<xml>
<name>xiaobai</name>
</xml>
`
const obj = xmlParser.parseString(xml)
new Parser(options)
options
\<Object> 配置对象trim
\<boolean> 去除文本节点头尾的空格。默认为false
。normalize
\<boolean> 去除文本节点内的空格。默认为false
。normalizeTags
\<boolean> 将所有标签名转成小写字母。默认为false
。ignoreAttrs
\<boolean> 忽略属性节点。默认为false
。attrkey
\<string> 设置属性节点的字段名。默认为$
。charkey
\<string> 设置文本节点的字段名。默认为_
。explicitArray
\<boolean> 保存子元素为数组。默认为true
。当设置为false
,只有出现多个相同标签名的子元素时才合并成数组。
parseString
解析 xml
数据为对象。
xml
\<string>
const xmlParser = new Parser({ explicitArray: false })
const xml = `
<user>
<name>xiaobai</name>
<age>12</age>
</user>
<user>
<name>xiaohong</name>
<age>11</age>
</user>
<count>2</count>
`
const result = xmlParser.parseString(xml)
/**
{
user: [
{
name: 'xiaobai',
age: '12'
},
{
name: 'xiaohong',
age: '11'
}
],
count: '2'
}
*/
new Builder(options)
options
\<Object> 配置对象rootName
\<string> 根节点名称。默认为root
。renderOpts
\<Object>pretty
\<boolean> 使用格式。默认为true
。indent
\<string> 缩进。默认为' '
。newline
\<string> 换行。默认\n
。
xmldec
\<Object>version
\<string> 头信息,版本号。默认1.0
。encoding
\<string> 头信息,编码格式。默认UTF-8
。standalone
\<boolean> 头信息,独立。默认true
。
headless
\<boolean> 去除头信息。默认false
。cdata
\<boolean> 当文本节点包含非法xml
字符时,用<![CDATA[……]]>
包含。默认false
。
buildObject
- obj \<Object> 要构建
xml
的对象。
const xmlBuilder = new Builder()
const obj = { name: 'xiaobai' }
const xml = xmlBuilder.buildObject(obj)
/**
<root>
<name>xiaobai</name>
</root>
*/
0.0.1
6 years ago