1# xml转换JavaScript 2 3> **说明:** 4> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 5 6 7## 导入模块 8 9``` 10import convertxml from '@ohos.convertxml'; 11``` 12 13## 系统能力 14 15SystemCapability.Utils.Lang 16 17## ConvertXML 18 19 20### convert 21 22convert(xml: string, options?: ConvertOptions) : Object 23 24转化xml文本为JavaScript对象。 25 26 27- 参数: 28 29 | 参数名 | 类型 | 必填 | 说明 | 30 | ------- | --------------------------------- | ---- | ------------------ | 31 | xml | string | 是 | 传入的xml文本。 | 32 | options | [ConvertOptions](#convertoptions) | 否 | 用户可进行的选项。 | 33 34- 返回值: 35 36 | 类型 | 说明 | 37 | ------ | ---------------------------- | 38 | Object | 处理后返回的JavaScript对象。 | 39 40- 示例: 41 42 ```js 43 let xml = 44 '<?xml version="1.0" encoding="utf-8"?>' + 45 '<note importance="high" logged="true">' + 46 ' <title>Happy</title>' + 47 ' <todo>Work</todo>' + 48 ' <todo>Play</todo>' + 49 '</note>'; 50 let conv = new convertxml.ConvertXML(); 51 let options = {trim : false, declarationKey:"_declaration", 52 instructionKey : "_instruction", attributesKey : "_attributes", 53 textKey : "_text", cdataKey:"_cdata", doctypeKey : "_doctype", 54 commentKey : "_comment", parentKey : "_parent", typeKey : "_type", 55 nameKey : "_name", elementsKey : "_elements"} 56 let result = JSON.stringify(conv.convert(xml, options)); 57 console.log(result) 58 ``` 59 60 61## ConvertOptions 62 63| 名称 | 参数类型 | 必填 | 说明 | 64| ----------------- | -------- | ---- | ----------------------------------------------------------- | 65| trim | boolean | 是 | 是否修剪位于文本前后的空白字符,默认false。 | 66| ignoreDeclaration | boolean | 否 | 是否忽略xml写入声明指示,默认false。 | 67| ignoreInstruction | boolean | 否 | 是否忽略xml的写入处理指令,默认false。 | 68| ignoreAttributes | boolean | 否 | 是否跨多行打印属性并缩进属性,默认false。 | 69| ignoreComment | boolean | 否 | 是否忽略元素的注释信息,默认false。 | 70| ignoreCDATA | boolean | 否 | 是否忽略元素的CDATA信息,默认false。 | 71| ignoreDoctype | boolean | 否 | 是否忽略元素的Doctype信息,默认false。 | 72| ignoreText | boolean | 否 | 是否忽略元素的文本信息,默认false。 | 73| declarationKey | string | 是 | 用于输出对象中declaration的属性键的名称,默认_declaration。 | 74| instructionKey | string | 是 | 用于输出对象中instruction的属性键的名称,默认_instruction。 | 75| attributesKey | string | 是 | 用于输出对象中attributes的属性键的名称,默认_attributes。 | 76| textKey | string | 是 | 用于输出对象中text的属性键的名称,默认_text。 | 77| cdataKey | string | 是 | 用于输出对象中cdata的属性键的名称,默认_cdata。 | 78| doctypeKey | string | 是 | 用于输出对象中doctype的属性键的名称,默认_doctype。 | 79| commentKey | string | 是 | 用于输出对象中comment的属性键的名称,默认_comment。 | 80| parentKey | string | 是 | 用于输出对象中parent的属性键的名称,默认_parent。 | 81| typeKey | string | 是 | 用于输出对象中type的属性键的名称,默认_type。 | 82| nameKey | string | 是 | 用于输出对象中name的属性键的名称,默认_name。 | 83| elementsKey | string | 是 | 用于输出对象中elements的属性键的名称,默认_elements。 |