• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# XML Conversion
2
3
4Converting XML text into JavaScript objects can simplify data handling and manipulation, making it more suitable for use in JavaScript applications.
5
6
7The ArkTS common library provides the **ConvertXML** class to convert XML text into JavaScript objects. The input is the XML string to be converted and conversion options, and the output is the resulting JavaScript object. For details about the conversion options, see [@ohos.convertxml (XML-to-JavaScript Conversion)](../reference/apis-arkts/js-apis-convertxml.md).
8
9
10## Precautions
11
12To ensure successful XML parsing and conversion, the input XML data must comply with the standard format.
13
14
15## How to Develop
16
17To convert an XML document into a JavaScript object and obtain the tag values, proceed as follows:
18
191. Import the module.
20
21   ```ts
22   import { convertxml } from '@kit.ArkTS';
23   ```
24
252. Input the XML document to be converted and set conversion options. For details about the supported conversion options and their meanings, see [ConvertOptions](../reference/apis-arkts/js-apis-convertxml.md#convertoptions).
26
27   > **NOTE**
28   >
29   > If the XML text to convert contains the ampersand (&), replace it with the entity reference **\&**.
30
31   ```ts
32   let xml: string =
33    '<?xml version="1.0" encoding="utf-8"?>' +
34    '<note importance="high" logged="true">' +
35    '    <title>Happy</title>' +
36    '    <todo>Work</todo>' +
37    '    <todo>Play</todo>' +
38    '</note>';
39   let options: convertxml.ConvertOptions = {
40     // trim: false, indicating that spaces before and after the text are not deleted after conversion.
41     // declarationKey: "_declaration", indicating that _declaration is used to identify the file declaration after conversion.
42     // instructionKey: "_instruction", indicating that _instruction is used to identify instructions after conversion.
43     // attributesKey: "_attributes", indicating that _attributes is used to identify attributes after conversion.
44     // textKey: "_text", indicating that _text is used to identify tag values after conversion.
45     // cdataKey: "_cdata", indicating that _cdata is used to identify unparsed data after conversion.
46     // docTypeKey: "_doctype", indicating that _doctype is used to identify documents after conversion.
47     // commentKey: "_comment", indicating that _comment is used to identify comments after conversion.
48     // parentKey: "_parent", indicating that _parent is used to identify parent classes after conversion.
49     // typeKey: "_type", indicating that _type is used to identify types after conversion.
50     // nameKey: "_name", indicating that _name is used to identify tag names after conversion.
51     // elementsKey: "_elements", indicating that _elements is used to identify elements after conversion.
52     trim: false,
53     declarationKey: "_declaration",
54     instructionKey: "_instruction",
55     attributesKey: "_attributes",
56     textKey: "_text",
57     cdataKey: "_cdata",
58     doctypeKey: "_doctype",
59     commentKey: "_comment",
60     parentKey: "_parent",
61     typeKey: "_type",
62     nameKey: "_name",
63     elementsKey: "_elements"
64   }
65   ```
66
673. Call the conversion function and print the result.
68
69   ```ts
70   let conv: convertxml.ConvertXML = new convertxml.ConvertXML();
71   let result: object = conv.fastConvertToJSObject(xml, options);
72   let strRes: string = JSON.stringify(result); // Convert the JavaScript object into a JSON string for explicit output.
73   console.info(strRes);
74   ```
75
76   The output is as follows:
77
78   ```json
79   strRes:
80   {"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}},"_elements":[{"_type":"element","_name":"note",
81    "_attributes":{"importance":"high","logged":"true"},"_elements":[{"_type":"element","_name":"title","_parent":"note",
82    "_elements":[{"_type":"text","_text":"Happy"}]},{"_type":"element","_name":"todo","_parent":"note","_elements":
83    [{"_type":"text","_text":"Work"}]},{"_type":"element","_name":"todo","_parent":"note","_elements":[{"_type":"text",
84    "_text":"Play"}]}]}]}
85   ```
86