【HarmonyOS NEXT】鸿蒙xml转换JavaScript

提供转换xml文本为JavaScript对象的功能。

导入模块


import { convertxml } from '@kit.ArkTS';

ConvertXML

convertToJSObject9+

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

转换xml文本为JavaScript对象。

元服务API:从API version 11 开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

错误码ID错误信息
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
10200002Invalid xml string.

示例:


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"}]}]}]}

convert(deprecated)

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

转换xml文本为JavaScript对象。

说明

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名类型必填说明
xmlstring传入的xml文本。
optionsConvertOptions转换选项 , 默认值是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: 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"}]}]}]}

ConvertOptions

转换选项。

元服务API:从API version 11 开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Utils.Lang

名称类型必填说明
trimboolean是否修剪位于文本前后的空白字符。
ignoreDeclarationboolean是否忽略xml写入声明指示,默认false。
ignoreInstructionboolean是否忽略xml的写入处理指令,默认false。
ignoreAttributesboolean是否跨多行打印属性并缩进属性,默认false。
ignoreCommentboolean是否忽略元素的注释信息,默认false。
ignoreCDATAboolean是否忽略元素的CDATA信息,默认false。
ignoreDoctypeboolean是否忽略元素的Doctype信息,默认false。
ignoreTextboolean是否忽略元素的文本信息,默认false。
declarationKeystring用于输出对象中declaration的属性键的名称。
instructionKeystring用于输出对象中instruction的属性键的名称。
attributesKeystring用于输出对象中attributes的属性键的名称。
textKeystring用于输出对象中text的属性键的名称。
cdataKeystring用于输出对象中cdata的属性键的名称
doctypeKeystring用于输出对象中doctype的属性键的名称。
commentKeystring用于输出对象中comment的属性键的名称。
parentKeystring用于输出对象中parent的属性键的名称。
typeKeystring用于输出对象中type的属性键的名称。
nameKeystring用于输出对象中name的属性键的名称。
elementsKeystring用于输出对象中elements的属性键的名称。
  • 17
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ConneyWu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值