• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# XML Conversion
2
3
4Converting XML text into JavaScript objects makes it easier to process and manipulate data. In addition, JavaScript objects are more suitable than XML text for JavaScript applications.
5
6
7The common library provides the **ConvertXML** class to convert XML text into JavaScript objects. The input is XML strings and conversion options, and the output is a JavaScript object. For details about the conversion options, see the API reference [@ohos.convertxml (XML-to-JavaScript Conversion)](../reference/apis/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
17The following steps walk you through on how to convert an XML file into a JavaScript object to obtain the tag values.
18
191. Import the **convertxml** module.
20
21   ```js
22   import convertxml from '@ohos.convertxml';
23   ```
24
252. Pass in an XML file to be converted and set conversion options.
26
27   ```js
28   let xml =
29     '<?xml version="1.0" encoding="utf-8"?>' +
30       '<note importance="high" logged="true">' +
31       '    <title>Happy</title>' +
32       '    <todo>Work</todo>' +
33       '    <todo>Play</todo>' +
34       '</note>';
35   let options = {
36     // trim: false, indicating that spaces before and after the text are not deleted after conversion.
37     // declarationKey: "_declaration", indicating that _declaration is used to identify the file declaration after conversion.
38     // instructionKey: "_instruction", indicating that _instruction is used to identify instructions after conversion.
39     // attributesKey: "_attributes", indicating that _attributes is used to identify attributes after conversion.
40     // textKey: "_text", indicating that _text is used to identify tag values after conversion.
41     // cdataKey: "_cdata", indicating that _cdata is used to identify unparsed data after conversion.
42     // docTypeKey: "_doctype", indicating that _doctype is used to identify documents after conversion.
43     // commentKey: "_comment", indicating that _comment is used to identify comments after conversion.
44     // parentKey: "_parent", indicating that _parent is used to identify parent classes after conversion.
45     // typeKey: "_type", indicating that _type is used to identify types after conversion.
46     // nameKey: "_name", indicating that _name is used to identify tag names after conversion.
47     // elementsKey: "_elements", indicating that _elements is used to identify elements after conversion.
48     trim: false,
49     declarationKey: "_declaration",
50     instructionKey: "_instruction",
51     attributesKey: "_attributes",
52     textKey: "_text",
53     cdataKey: "_cdata",
54     docTypeKey: "_doctype",
55     commentKey: "_comment",
56     parentKey: "_parent",
57     typeKey: "_type",
58     nameKey: "_name",
59     elementsKey: "_elements"
60   }
61   ```
62
633. Call the conversion function and print the result.
64
65   ```js
66   let conv = new convertxml.ConvertXML();
67   let result = conv.convertToJSObject(xml, options);
68   let strRes = JSON.stringify(result); // Convert the JavaScript object into a JSON string for explicit output.
69   console.info(strRes);
70   // Alternatively, directly process the JavaScript object to obtain the tag values.
71   let title = result['_elements'][0]['_elements'][0]['_elements'][0]['_text']; // Parse the value of the <title> tag.
72   let todo = result['_elements'][0]['_elements'][1]['_elements'][0]['_text']; // Parse the value of the <todo> tag.
73   let todo2 = result['_elements'][0]['_elements'][2]['_elements'][0]['_text']; // Parse the value of the <todo> tag.
74   console.info(title); // Happy
75   console.info(todo); // Work
76   console.info(todo2); // Play
77   ```
78
79   The output is as follows:
80
81
82   ```js
83   strRes:
84   {"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}},"_elements":[{"_type":"element","_name":"note",
85    "_attributes":{"importance":"high","logged":"true"},"_elements":[{"_type":"element","_name":"title",
86    "_elements":[{"_type":"text","_text":"Happy"}]},{"_type":"element","_name":"todo",
87    "_elements":[{"_type":"text","_text":"Work"}]},{"_type":"element","_name":"todo",
88    "_elements":[{"_type":"text","_text":"Play"}]}]}]}
89   title:Happy
90   todo:Work
91   todo2:Play
92   ```
93