ktxml

1.0.6 • Public • Published

XML解析器

安装方法

npm install ktxml

该库依赖于: xmlreader, 请先安装依赖

npm install xmlreader

使用方法

以下方法均以该例子进行举例:

 <response id="1" shop="aldi">
  This is some other content
  <who name="james">James May</who>
  <who name="sam">
    Sam Decrock
    <location>Belgium</location>
  </who>
  <who name="jack">Jack Johnsen</who>
  <games age="6">
    <game>Some great game</game>
    <game>Some other great game</game>
  </games>
  <note>These are some notes</note>
</response>

1. 获取xml节点的字段

请注意, 数组类型和普通类型请区别对待. 数组使用getArrayText()

* 获取XML节点字段
* @param {string} xmlString xml字符串
* @param {string} param 字段参数
* @param {any} success 成功
* @param {any} failed 失败
getText(string, string, success, failed)

例子:

// 获取response 节点中的内容
ktxml.getText(xml_string, "response", (res)=>{
  console.log("SUCCESS=>" + res)
},
(err)=>{
  console.log("FAILED=>" + err)
})

得到回复:

SUCCESS=>This is some other content

2. 获取xml数组节点中的字段

请注意, 数组类型和普通类型请区别对待. 普通类型请用getText()

* 获取XML节点数组中的字段
* @param {string} xmlString xml字符串
* @param {string} param 字段参数
* @param {int} index 数组index
* @param {any} success 成功
* @param {any} failed 失败
getArrayText(xmlString, param, index, success, failed)

例子:

// 获取games中的game数组的第0项的内容
 
ktxml.getArrayText(xml_string, "response.games.game", 0, (res)=>{
  console.log("SUCCESS=>" + res)
}, (err)=>{
  console.log("FAILED=>" + err)
} )

得到回复

SUCCESS=>Some great game

3. 获取XML节点中的属性内容(成功回调格式为json)

请注意, 数组类型和普通类型请区别对待. 数组使用getArrayAttr()

* 获取XML中节点的属性
* @param {string} xmlString xml字段
* @param {string} param 参数
* @param {any} success 成功
* @param {any} failed 失败
getAttr(xmlString, param,success, failed)

例子:

//获取xml字段中 response的属性
 
ktxml.getAttr(xml_string, "response", (res)=>{
  console.log("SUCCESS=>" , res)
},
(err)=>{
  console.log("FAILED=>" + err)
})

得到回复(JSON)

SUCCESS=> { id: '1', shop: 'aldi' }

4. 获取XML数组节点中的属性内容(成功回调格式为json)

请注意, 数组类型和普通类型请区别对待. 普通类型使用: getAttr()

* 获取XML数组节点的属性
* @param {string} xmlString xml字段
* @param {string} param 参数
* @param {int} index 数组index
* @param {any} success 成功
* @param {any} failed 失败
getArrayAttr(xmlString, param, index, success, failed)

例子:

// 获取xml字段中 response中 who 数组 第1位的数据
ktxml.getArrayAttr(xml_string, "response.who", 1, (res)=>{
  console.log("SUCCESS=>" , res)
},
(err)=>{
  console.log("FAILED=>" + err)
})

得到回复(JSON)

SUCCESS=> { name: 'sam' }

5. 获取xml中的array方法

请注意, 获取了以后请自行查看需要使用的数据类型方法, 例如 获取字段 请用 .text() 获取属性名称请用 .attributes()

* 获取XML数组节点的属性
* @param {string} xmlString xml字段
* @param {string} param 参数
* @param {any} success 成功
* @param {any} failed 失败
getArray(xmlString, param, success, failed)

例子:

// 获取xml字段中 response中 who 数组 第1位的数据
ktxml.getArray(xml_string, "response.who", (res)=>{
  console.log("SUCCESS=>" , res)
  console.og(res[0].text())
},
(err)=>{
  console.log("FAILED=>" + err)
})

得到回复(JSON)

...
James May
库中使用到了 eval. 十分危险, 谨防注入

Readme

Keywords

none

Package Sidebar

Install

npm i ktxml

Weekly Downloads

0

Version

1.0.6

License

ISC

Unpacked Size

8.26 kB

Total Files

3

Last publish

Collaborators

  • fanyifang