js-apis-convertxml.md 7.1 KB
Newer Older
W
wusongqing 已提交
1
# XML-to-JavaScript Conversion
Z
zengyawen 已提交
2

W
wusongqing 已提交
3 4 5 6
The **convertxml** module provides APIs for converting XML text into JavaScript objects.

> **NOTE**
>
W
wusongqing 已提交
7
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

W
wusongqing 已提交
9 10

## Modules to Import
Z
zengyawen 已提交
11

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

W
wusongqing 已提交
16
## ConvertXML
Z
zengyawen 已提交
17

G
Gloria 已提交
18
### convertToJSObject<sup>9+</sup>
Z
zengyawen 已提交
19

G
Gloria 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
convertToJSObject(xml: string, options?: ConvertOptions) : Object

Converts an XML text into a JavaScript object.

**System capability**: SystemCapability.Utils.Lang

**Parameters**

| Name | Type                             | Mandatory| Description           |
| ------- | --------------------------------- | ---- | --------------- |
| xml     | string                            | Yes  | XML text to convert.|
| options | [ConvertOptions](#convertoptions) | No  | Options for conversion.     |

**Return value**

| Type  | Description                        |
| ------ | ---------------------------- |
| Object | JavaScript object.|

**Example**

```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.convertToJSObject();
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);
// Output (non-compact)
// {"_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>

> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [convertToJSObject9+](#converttojsobject9) instead.
Z
zengyawen 已提交
66

W
wusongqing 已提交
67
convert(xml: string, options?: ConvertOptions) : Object
Z
zengyawen 已提交
68

W
wusongqing 已提交
69
Converts an XML text into a JavaScript object.
Z
zengyawen 已提交
70

W
wusongqing 已提交
71
**System capability**: SystemCapability.Utils.Lang
Z
zengyawen 已提交
72

W
wusongqing 已提交
73
**Parameters**
W
wusongqing 已提交
74

W
wusongqing 已提交
75 76 77
| Name | Type                             | Mandatory| Description           |
| ------- | --------------------------------- | ---- | --------------- |
| xml     | string                            | Yes  | XML text to convert.|
N
Neil Chen 已提交
78
| options | [ConvertOptions](#convertoptions) | No  | Options for conversion.     |
W
wusongqing 已提交
79

W
wusongqing 已提交
80
**Return value**
W
wusongqing 已提交
81

W
wusongqing 已提交
82 83 84
| Type  | Description                        |
| ------ | ---------------------------- |
| Object | JavaScript object.|
W
wusongqing 已提交
85

W
wusongqing 已提交
86
**Example**
W
wusongqing 已提交
87

W
wusongqing 已提交
88 89 90 91 92 93 94 95 96 97
```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",
W
wusongqing 已提交
98 99 100 101
    instructionKey : "_instruction", attributesKey : "_attributes",
    textKey : "_text", cdataKey:"_cdata", doctypeKey : "_doctype",
    commentKey : "_comment", parentKey : "_parent", typeKey : "_type",
    nameKey : "_name", elementsKey : "_elements"}
W
wusongqing 已提交
102
let result = JSON.stringify(conv.convert(xml, options));
G
Gloria 已提交
103 104
console.log(result);
// Output (non-compact)
105
// {"_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"}]}]}]}
W
wusongqing 已提交
106
```
W
wusongqing 已提交
107 108 109

## ConvertOptions

N
Neil Chen 已提交
110
Options for conversion.
W
wusongqing 已提交
111 112 113 114

**System capability**: SystemCapability.Utils.Lang

| Name             | Type| Mandatory| Description                                                       |
W
wusongqing 已提交
115
| ----------------- | -------- | ---- | ----------------------------------------------------------- |
W
wusongqing 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| trim              | boolean  | Yes  | Whether to trim the whitespace characters before and after the text. The default value is **false**.                |
| ignoreDeclaration | boolean  | No  | Whether to ignore the XML declaration. The default value is **false**.                       |
| ignoreInstruction | boolean  | No  | Whether to ignore the XML processing instruction. The default value is **false**.                     |
| ignoreAttributes  | boolean  | No  | Whether to print attributes across multiple lines and indent attributes. The default value is **false**.                  |
| ignoreComment     | boolean  | No  | Whether to ignore element comments. The default value is **false**.                        |
| ignoreCDATA       | boolean  | No  | Whether to ignore the element's CDATA information. The default value is **false**.                       |
| ignoreDoctype     | boolean  | No  | Whether to ignore the element's Doctype information. The default value is **false**.                     |
| ignoreText        | boolean  | No  | Whether to ignore the element's text information. The default value is **false**.                        |
| declarationKey    | string   | Yes  | Name of the attribute key for **declaration** in the output object. The default value is **_declaration**.|
| instructionKey    | string   | Yes  | Name of the attribute key for **instruction** in the output object. The default value is **_instruction**.|
| attributesKey     | string   | Yes  | Name of the attribute key for **attributes** in the output object. The default value is **_attributes**.  |
| textKey           | string   | Yes  | Name of the attribute key for **text** in the output object. The default value is **_text**.              |
| cdataKey          | string   | Yes  | Name of the attribute key for **CDATA** in the output object. The default value is **_cdata**.            |
| doctypeKey        | string   | Yes  | Name of the attribute key for **Doctype** in the output object. The default value is **_doctype**.        |
| commentKey        | string   | Yes  | Name of the attribute key for **comment** in the output object. The default value is **_comment**.        |
| parentKey         | string   | Yes  | Name of the attribute key for **parent** in the output object. The default value is **_parent**.          |
| typeKey           | string   | Yes  | Name of the attribute key for **type** in the output object. The default value is **_type**.              |
| nameKey           | string   | Yes  | Name of the attribute key for **name** in the output object. The default value is **_name**.              |
| elementsKey       | string   | Yes  | Name of the attribute key for **elements** in the output object. The default value is **_elements**.      |