• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# XML-to-JavaScript Conversion
2
3> **NOTE**
4> 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.
5
6
7## Modules to Import
8
9```
10import convertxml from '@ohos.convertxml';
11```
12
13## System Capabilities
14
15SystemCapability.Utils.Lang
16
17## ConvertXML
18
19
20### convert
21
22convert(xml: string, options?: ConvertOptions) : Object
23
24Converts an XML text into a JavaScript object.
25
26
27- Parameters
28
29  | Name| Type| Mandatory| Description|
30  | ------- | --------------------------------- | ---- | ------------------ |
31  | xml     | string                            | Yes| XML text to convert.|
32  | options | [ConvertOptions](#convertoptions) | No| Options for coversion.|
33
34- Return value
35
36  | Type| Description|
37  | ------ | ---------------------------- |
38  | Object | JavaScript object.|
39
40- Example
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| Name| Type| Mandatory| Description|
64| ----------------- | -------- | ---- | ----------------------------------------------------------- |
65| trim              | boolean  | Yes| Whether to trim the whitespace characters before and after the text. The default value is **false**.|
66| ignoreDeclaration | boolean  | No| Whether to ignore the XML declaration. The default value is **false**.|
67| ignoreInstruction | boolean  | No| Whether to ignore the XML processing instruction. The default value is **false**.|
68| ignoreAttributes  | boolean  | No| Whether to print attributes across multiple lines and indent attributes. The default value is **false**.|
69| ignoreComment     | boolean  | No| Whether to ignore element comments. The default value is **false**.|
70| ignoreCDATA       | boolean  | No| Whether to ignore the element's CDATA information. The default value is **false**.|
71| ignoreDoctype     | boolean  | No| Whether to ignore the element's Doctype information. The default value is **false**.|
72| ignoreText        | boolean  | No| Whether to ignore the element's text information. The default value is **false**.|
73| declarationKey    | string   | Yes| Name of the attribute key for **declaration** in the output object. The default value is **_declaration**.|
74| instructionKey    | string   | Yes| Name of the attribute key for **instruction** in the output object. The default value is **_instruction**.|
75| attributesKey     | string   | Yes| Name of the attribute key for **attributes** in the output object. The default value is **_attributes**.|
76| textKey           | string   | Yes| Name of the attribute key for **text** in the output object. The default value is **_text**.|
77| cdataKey          | string   | Yes| Name of the attribute key for **CDATA** in the output object. The default value is **_cdata**.|
78| doctypeKey        | string   | Yes| Name of the attribute key for **Doctype** in the output object. The default value is **_doctype**.|
79| commentKey        | string   | Yes| Name of the attribute key for **comment** in the output object. The default value is **_comment**.|
80| parentKey         | string   | Yes| Name of the attribute key for **parent** in the output object. The default value is **_parent**.|
81| typeKey           | string   | Yes| Name of the attribute key for **type** in the output object. The default value is **_type**.|
82| nameKey           | string   | Yes| Name of the attribute key for **name** in the output object. The default value is **_name**.|
83| elementsKey       | string   | Yes| Name of the attribute key for **elements** in the output object. The default value is **_elements**.|
84