js-apis-convertxml.md 6.9 KB
Newer Older
1
# @ohos.convertxml (xml转换JavaScript)
Z
zengyawen 已提交
2

3
本模块提供转换xml文本为JavaScript对象的功能。
T
explain  
tianyu 已提交
4

Z
zengyawen 已提交
5
> **说明:**
6
>
Z
zengyawen 已提交
7 8 9 10 11
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。


## 导入模块

Z
zengyawen 已提交
12
```js
Z
zengyawen 已提交
13
import convertxml from '@ohos.convertxml';
Z
zengyawen 已提交
14 15 16 17
```

## ConvertXML

J
jiangkai43 已提交
18
### convertToJSObject<sup>9+</sup>
Z
zengyawen 已提交
19

J
jiangkai43 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
convertToJSObject(xml: string, options?: ConvertOptions) : Object

转换xml文本为JavaScript对象。

**系统能力:** SystemCapability.Utils.Lang

**参数:**

| 参数名  | 类型                              | 必填 | 说明            |
| ------- | --------------------------------- | ---- | --------------- |
| xml     | string                            | 是   | 传入的xml文本。 |
| options | [ConvertOptions](#convertoptions) | 否   | 转换选项。      |

**返回值:**

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

39 40 41 42 43 44 45 46
**错误码:**

以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200002 | Invalid xml string. |

J
jiangkai43 已提交
47 48 49
**示例:**

```js
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
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 = {
        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.toString());
}
J
jiangkai43 已提交
71 72 73 74 75 76
// 输出(宽泛型)
// {"_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<sup>(deprecated)</sup>

Z
zengyawen 已提交
77 78
convert(xml: string, options?: ConvertOptions) : Object

Z
zengyawen 已提交
79
转换xml文本为JavaScript对象。
Z
zengyawen 已提交
80

81 82 83 84
> **说明:**
>
> 从API version 8开始支持,从API version 9开始废弃,建议使用[convertToJSObject<sup>9+</sup>](#converttojsobject9)替代。

Z
zengyawen 已提交
85
**系统能力:** SystemCapability.Utils.Lang
Z
zengyawen 已提交
86

Z
zengyawen 已提交
87
**参数:**
Z
zengyawen 已提交
88

Z
zengyawen 已提交
89 90 91 92
| 参数名  | 类型                              | 必填 | 说明            |
| ------- | --------------------------------- | ---- | --------------- |
| xml     | string                            | 是   | 传入的xml文本。 |
| options | [ConvertOptions](#convertoptions) | 否   | 转换选项。      |
Z
zengyawen 已提交
93

Z
zengyawen 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
**返回值:**

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

**示例:**

```js
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",
112 113 114 115
    instructionKey : "_instruction", attributesKey : "_attributes",
    textKey : "_text", cdataKey:"_cdata", doctypeKey : "_doctype",
    commentKey : "_comment", parentKey : "_parent", typeKey : "_type",
    nameKey : "_name", elementsKey : "_elements"}
Z
zengyawen 已提交
116
let result = JSON.stringify(conv.convert(xml, options));
117 118 119
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"}]}]}]}
Z
zengyawen 已提交
120
```
Z
zengyawen 已提交
121 122 123

## ConvertOptions

Z
zengyawen 已提交
124 125
转换选项。

126
**系统能力:** SystemCapability.Utils.Lang
Z
zengyawen 已提交
127

128
| 名称              | 类型 | 必填 | 说明                                                        |
Z
zengyawen 已提交
129
| ----------------- | -------- | ---- | ----------------------------------------------------------- |
L
lihucheng 已提交
130
| trim              | boolean  | 是   | 是否修剪位于文本前后的空白字符,默认false。                 |
Z
zengyawen 已提交
131 132 133 134
| ignoreDeclaration | boolean  | 否   | 是否忽略xml写入声明指示,默认false。                        |
| ignoreInstruction | boolean  | 否   | 是否忽略xml的写入处理指令,默认false。                      |
| ignoreAttributes  | boolean  | 否   | 是否跨多行打印属性并缩进属性,默认false。                   |
| ignoreComment     | boolean  | 否   | 是否忽略元素的注释信息,默认false。                         |
Z
zengyawen 已提交
135
| ignoreCDATA       | boolean  | 否   | 是否忽略元素的CDATA信息,默认false。                        |
Z
zengyawen 已提交
136 137
| ignoreDoctype     | boolean  | 否   | 是否忽略元素的Doctype信息,默认false。                      |
| ignoreText        | boolean  | 否   | 是否忽略元素的文本信息,默认false。                         |
L
lihucheng 已提交
138 139 140 141 142 143 144 145 146 147 148
| 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。       |