HarmonyOS xml转换JavaScript 常用的几个方法

HarmonyOS 使用 xml转换JavaScript 的好处

  1. 易用性

    • 提供了简洁的API接口,使得XML到JavaScript对象的转换变得简单直接。
    • 转换选项的灵活性允许开发者根据实际需求自定义转换结果。
  2. 高效性

    • HarmonyOS对底层运行时环境进行了优化,使得XML转换过程更加高效。
    • 转换后的JavaScript对象可以直接在JavaScript应用程序中使用,无需额外的处理或转换。
  3. 兼容性

    • 支持多种XML格式和结构的转换。
    • 与HarmonyOS的其他组件和API高度兼容,便于集成和使用。
  4. 可扩展性

    • 开发者可以根据需要扩展转换功能,例如添加自定义的转换规则或处理逻辑。
    • 转换选项的扩展性使得未来的需求变化可以轻松应对。
  5. 维护性

    • 转换逻辑被封装在ConvertXML类中,使得代码更加模块化和易于维护。
    • 提供了详细的API文档和示例代码,有助于开发者快速上手和解决问题。
      专为开发者打造的高效XML到JavaScript对象转换模块,能够轻松解析复杂的XML文本,并将其转换为结构化的JavaScript对象,支持嵌套元素、属性和文本内容的处理。无论是Web开发、移动应用还是数据分析场景,本模块都能助您一臂之力,实现数据的快速处理和展示,提升开发效率。

xml转换JavaScript

1. ConvertOptions

2. ConvertXML

1. convertToJSObject

2. convert

1. ConvertOptions

转换选项。

参数:

名称 类型 必填 说明
trim boolean 是否修剪位于文本前后的空白字符。
ignoreDeclaration boolean 是否忽略xml写入声明指示,默认false。
ignoreInstruction boolean 是否忽略xml的写入处理指令,默认false。
ignoreAttributes boolean 是否忽略元素的属性信息,默认false。
ignoreComment boolean 是否忽略元素的注释信息,默认false。
ignoreCDATA boolean 是否忽略元素的CDATA信息,默认false。
ignoreDoctype boolean 是否忽略元素的Doctype信息,默认false。
ignoreText boolean 是否忽略元素的文本信息,默认false。
declarationKey string 用于输出对象中declaration的属性键的名称。
instructionKey string 用于输出对象中instruction的属性键的名称。
attributesKey string 用于输出对象中attributes的属性键的名称。
textKey string 用于输出对象中text的属性键的名称。
cdataKey string 用于输出对象中cdata的属性键的名称
doctypeKey string 用于输出对象中doctype的属性键的名称。
commentKey string 用于输出对象中comment的属性键的名称。
parentKey string 用于输出对象中parent的属性键的名称。
typeKey string 用于输出对象中type的属性键的名称。
nameKey string 用于输出对象中name的属性键的名称。
elementsKey string 用于输出对象中elements的属性键的名称。

2. ConvertXML

1. convertToJSObject

javascript 复制代码
convertToJSObject(xml: string, options?: ConvertOptions) : Object

转换xml文本为JavaScript对象。

参数:

参数名 类型 必填 说明
xml string 传入的xml文本。
options ConvertOptions 转换选项 , 默认值是ConvertOptions对象 , 由其中各个属性的默认值组成。

返回值:

类型 说明
Object 处理后返回的JavaScript对象。

使用方式:

javascript 复制代码
try {
  let xml =
    '<?xml version="1.0" encoding="utf-8"?>' +
      '<note importance="high" logged="true">' +
      '    <title>Happy</title>' +
      '    <todo>Work</todo>' +
      '    <todo>Play</todo>' +
      '</note>';
  let conv = new convertxml.ConvertXML()
  let options: convertxml.ConvertOptions = {
    trim: false, declarationKey: "_declaration",
    instructionKey: "_instruction", attributesKey: "_attributes",
    textKey: "_text", cdataKey: "_cdata", doctypeKey: "_doctype",
    commentKey: "_comment", parentKey: "_parent", typeKey: "_type",
    nameKey: "_name", elementsKey: "_elements"
  }
  let result = JSON.stringify(conv.convertToJSObject(xml, options));
  console.log(result);
} catch (e) {
  console.log((e as Object).toString());
}

输出:

// 输出(宽泛型)

// {"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}},"_elements":{"_type":"element","_name":"note","_attributes":{"importance":"high","logged":"true"},"_elements":\[{"_type":"element","_name":"title","_elements":\[{"_type":"text","_text":"Happy"}},{"_type":"element","_name":"todo","_elements":{"_type":"text","_text":"Work"}},{"_type":"element","_name":"todo","_elements":{"_type":"text","_text":"Play"}}]}]}

2. convert

javascript 复制代码
convert(xml: string, options?: ConvertOptions) : Object

转换xml文本为JavaScript对象。
说明

从API version 8开始支持,从API version 9开始废弃,建议使用convertToJSObject9+替代。

参数:

参数名 类型 必填 说明
xml string 传入的xml文本。
options ConvertOptions 转换选项 , 默认值是ConvertOptions对象 , 由其中各个属性的默认值组成。

返回值:

类型 说明
Object 处理后返回的JavaScript对象。

使用方式:

javascript 复制代码
let xml =
  '<?xml version="1.0" encoding="utf-8"?>' +
    '<note importance="high" logged="true">' +
    '    <title>Happy</title>' +
    '    <todo>Work</todo>' +
    '    <todo>Play</todo>' +
    '</note>';
let conv = new convertxml.ConvertXML();
let options: convertxml.ConvertOptions = {trim : false, declarationKey:"_declaration",
  instructionKey : "_instruction", attributesKey : "_attributes",
  textKey : "_text", cdataKey:"_cdata", doctypeKey : "_doctype",
  commentKey : "_comment", parentKey : "_parent", typeKey : "_type",
  nameKey : "_name", elementsKey : "_elements"}
let result = JSON.stringify(conv.convert(xml, options));
console.log(result);

输出:

// 输出(宽泛型)

// {"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}},"_elements":{"_type":"element","_name":"note","_attributes":{"importance":"high","logged":"true"},"_elements":\[{"_type":"element","_name":"title","_elements":\[{"_type":"text","_text":"Happy"}},{"_type":"element","_name":"todo","_elements":{"_type":"text","_text":"Work"}},{"_type":"element","_name":"todo","_elements":{"_type":"text","_text":"Play"}}]}]}

还有其他问题 请参考官方文档

制作不易 点个关注再走吧。°(°¯᷄◠¯᷅°)°。

相关推荐
kyriewen15 小时前
我排查了一个React内存泄漏——罪魁祸首是这3个被忽略的清理函数
前端·javascript·面试
雨田言炎15 小时前
四、关于Qt项目需要知道的
开发语言·笔记·qt
其美杰布-富贵-李16 小时前
03 ref、reactive 与 computed 响应式数据
前端·javascript·vue.js
用户9385156350716 小时前
useRef + Web Worker 实战:React 如何优雅地拥抱多线程
前端·javascript·react.js
程序喵大人16 小时前
【C++进阶】STL算法与函数对象 - 02 sort为什么需要随机访问迭代器
开发语言·c++·算法
Source.Liu16 小时前
【Uppsala】入口模块(lib.rs)
xml·rust
嘟嘟071716 小时前
useRef + Web Worker:React 中的耗时计算不卡页面
javascript·vue.js
mONESY16 小时前
React Router v7 完全实战指南:从入门到鉴权,一套代码全搞定
javascript
SomeB1oody16 小时前
【RustyML入门】2.6. 线性判别分析
开发语言·后端·机器学习·rust·教程
To_OC16 小时前
LC 74 搜索二维矩阵:换皮的二分查找,我居然一开始没看出来
javascript·算法·leetcode