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

W
wusongqing 已提交
3 4
> **NOTE**
> 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 已提交
5

W
wusongqing 已提交
6 7

## Modules to Import
Z
zengyawen 已提交
8 9 10 11 12

```
import convertxml from '@ohos.convertxml';
```

W
wusongqing 已提交
13
## System Capabilities
Z
zengyawen 已提交
14

W
wusongqing 已提交
15
SystemCapability.Utils.Lang
Z
zengyawen 已提交
16

W
wusongqing 已提交
17
## ConvertXML
Z
zengyawen 已提交
18 19


W
wusongqing 已提交
20
### convert
Z
zengyawen 已提交
21

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

W
wusongqing 已提交
24
Converts an XML text into a JavaScript object.
Z
zengyawen 已提交
25 26


W
wusongqing 已提交
27 28 29 30 31 32 33 34 35 36 37
- Parameters

  | Name| Type| Mandatory| Description|
  | ------- | --------------------------------- | ---- | ------------------ |
  | xml     | string                            | Yes| XML text to convert.|
  | options | [ConvertOptions](#convertoptions) | No| Settings of the convert operation.|

- Return value

  | Type| Description|
  | ------ | ---------------------------- |
L
lihucheng 已提交
38
  | Object | JavaScript object.|
W
wusongqing 已提交
39 40 41

- Example

42
  ```js
L
lihucheng 已提交
43
  let xml =
W
wusongqing 已提交
44 45 46 47 48 49
      '<?xml version="1.0" encoding="utf-8"?>' +
      '<note importance="high" logged="true">' +
      '    <title>Happy</title>' +
      '    <todo>Work</todo>' +
      '    <todo>Play</todo>' +
      '</note>';
L
lihucheng 已提交
50
  let conv = new convertxml.ConvertXML();
L
lihucheng 已提交
51 52 53 54 55 56 57
  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)
W
wusongqing 已提交
58 59 60 61 62 63 64
  ```


## ConvertOptions

| Name| Type| Mandatory| Description|
| ----------------- | -------- | ---- | ----------------------------------------------------------- |
L
lihucheng 已提交
65
| trim              | boolean  | Yes| Whether to trim the whitespace characters before and after the text. The default value is **false**.|
W
wusongqing 已提交
66 67 68 69 70 71 72
| 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**.|
L
lihucheng 已提交
73 74 75 76 77 78 79 80 81 82 83
| 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**.|