鸿蒙语言基础类库:【@ohos.convertxml (xml转换JavaScript)】

xml转换JavaScript

说明: 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。开发前请熟悉鸿蒙开发指导文档gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md点击或者复制转到。

导入模块

复制代码
import convertxml from '@ohos.convertxml';

ConvertXML

convert

convert(xml: string, options?: ConvertOptions) : Object

转换xml文本为JavaScript对象。

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
xml string 传入的xml文本。
options [ConvertOptions] 转换选项。

返回值:

类型 说明
Object 处理后返回的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 = {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)

ConvertOptions

转换选项。

系统能力: 以下各项对应的系统能力均为SystemCapability.Utils.Lang

名称 参数类型 必填 说明 HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿
trim boolean 是否修剪位于文本前后的空白字符,默认false。
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的属性键的名称,默认_declaration。
instructionKey string 用于输出对象中instruction的属性键的名称,默认_instruction。
attributesKey string 用于输出对象中attributes的属性键的名称,默认_attributes。
textKey string 用于输出对象中text的属性键的名称,默认_text。
cdataKey string 用于输出对象中cdata的属性键的名称,默认_cdata。
doctypeKey string 用于输出对象中doctype的属性键的名称,默认_doctype。
commentKey string 用于输出对象中comment的属性键的名称,默认_comment。
parentKey string 用于输出对象中parent的属性键的名称,默认_parent。
typeKey string 用于输出对象中type的属性键的名称,默认_type。
nameKey string 用于输出对象中name的属性键的名称,默认_name。
elementsKey string 用于输出对象中elements的属性键的名称,默认_elements。
相关推荐
哈__41 分钟前
ReactNative项目OpenHarmony三方库集成实战:react-native-device-info
javascript·react native·react.js
庄小焱1 小时前
React——React基础语法(2)
前端·javascript·react.js
终端鹿1 小时前
Vue3 核心 API 深度解析:ref / reactive / computed / watch
前端·javascript·vue.js
大雷神1 小时前
HarmonyOS APP<玩转React>开源教程十五:首页完整实现
react.js·开源·harmonyos
console.log('npc')1 小时前
partial在react接口定义中是什么意思
前端·javascript·typescript
SuperEugene1 小时前
前端 utils 工具函数规范:拆分 / 命名 / 复用全指南,避开全局污染等高频坑|编码语法规范篇
开发语言·前端·javascript
Amumu121381 小时前
Js:内置对象
开发语言·前端·javascript
云和数据.ChenGuang2 小时前
鸿蒙智联,极智共生:HarmonyOS与MiniMax智能体的融合新纪元
华为·harmonyos·鸿蒙
我命由我123452 小时前
Element Plus 2.2.27 的单选框 Radio 组件,选中一个选项后,全部选项都变为选中状态
开发语言·前端·javascript·html·ecmascript·html5·js
不爱吃糖的程序媛2 小时前
已有 Flutter 应用适配鸿蒙平台指导文档
flutter·华为·harmonyos