XML解析器
安装方法
npm install ktxml
该库依赖于: xmlreader, 请先安装依赖
npm install xmlreader
使用方法
以下方法均以该例子进行举例:
This is some other content James May Sam Decrock Belgium Jack Johnsen Some great game Some other great game These are some notes
1. 获取xml节点的字段
请注意, 数组类型和普通类型请区别对待. 数组使用getArrayText()
* 获取XML节点字段* @param string xmlString xml字符串* @param string param 字段参数* @param any success 成功* @param any failed 失败
例子:
// 获取response 节点中的内容ktxml
得到回复:
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 失败
例子:
// 获取games中的game数组的第0项的内容 ktxml
得到回复
SUCCESS=>Some great game
3. 获取XML节点中的属性内容(成功回调格式为json)
请注意, 数组类型和普通类型请区别对待. 数组使用getArrayAttr()
* 获取XML中节点的属性* @param string xmlString xml字段* @param string param 参数* @param any success 成功* @param any failed 失败
例子:
//获取xml字段中 response的属性 ktxml
得到回复(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 失败
例子:
// 获取xml字段中 response中 who 数组 第1位的数据ktxml
得到回复(JSON)
SUCCESS=> { name: 'sam' }
5. 获取xml中的array方法
请注意, 获取了以后请自行查看需要使用的数据类型方法, 例如 获取字段 请用 .text() 获取属性名称请用 .attributes()
* 获取XML数组节点的属性* @param string xmlString xml字段* @param string param 参数* @param any success 成功* @param any failed 失败
例子:
// 获取xml字段中 response中 who 数组 第1位的数据ktxml
得到回复(JSON)
...
James May