1e41f4b71Sopenharmony_ci# XML Conversion 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciConverting 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. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciThe 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 [@ohos.convertxml (XML-to-JavaScript Conversion)](../reference/apis-arkts/js-apis-convertxml.md). 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## Precautions 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ciTo ensure successful XML parsing and conversion, the input XML data must comply with the standard format. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## How to Develop 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciTo convert an XML file into a JavaScript object to obtain the tag values, proceed as follows: 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci1. Import the **convertxml** module. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci ```ts 22e41f4b71Sopenharmony_ci import { convertxml } from '@kit.ArkTS'; 23e41f4b71Sopenharmony_ci ``` 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci2. Pass in the XML file to be converted and set conversion options. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci ```ts 28e41f4b71Sopenharmony_ci let xml: string = 29e41f4b71Sopenharmony_ci '<?xml version="1.0" encoding="utf-8"?>' + 30e41f4b71Sopenharmony_ci '<note importance="high" logged="true">' + 31e41f4b71Sopenharmony_ci ' <title>Happy</title>' + 32e41f4b71Sopenharmony_ci ' <todo>Work</todo>' + 33e41f4b71Sopenharmony_ci ' <todo>Play</todo>' + 34e41f4b71Sopenharmony_ci '</note>'; 35e41f4b71Sopenharmony_ci let options: convertxml.ConvertOptions = { 36e41f4b71Sopenharmony_ci // trim: false, indicating that spaces before and after the text are not deleted after conversion. 37e41f4b71Sopenharmony_ci // declarationKey: "_declaration", indicating that _declaration is used to identify the file declaration after conversion. 38e41f4b71Sopenharmony_ci // instructionKey: "_instruction", indicating that _instruction is used to identify instructions after conversion. 39e41f4b71Sopenharmony_ci // attributesKey: "_attributes", indicating that _attributes is used to identify attributes after conversion. 40e41f4b71Sopenharmony_ci // textKey: "_text", indicating that _text is used to identify tag values after conversion. 41e41f4b71Sopenharmony_ci // cdataKey: "_cdata", indicating that _cdata is used to identify unparsed data after conversion. 42e41f4b71Sopenharmony_ci // docTypeKey: "_doctype", indicating that _doctype is used to identify documents after conversion. 43e41f4b71Sopenharmony_ci // commentKey: "_comment", indicating that _comment is used to identify comments after conversion. 44e41f4b71Sopenharmony_ci // parentKey: "_parent", indicating that _parent is used to identify parent classes after conversion. 45e41f4b71Sopenharmony_ci // typeKey: "_type", indicating that _type is used to identify types after conversion. 46e41f4b71Sopenharmony_ci // nameKey: "_name", indicating that _name is used to identify tag names after conversion. 47e41f4b71Sopenharmony_ci // elementsKey: "_elements", indicating that _elements is used to identify elements after conversion. 48e41f4b71Sopenharmony_ci trim: false, 49e41f4b71Sopenharmony_ci declarationKey: "_declaration", 50e41f4b71Sopenharmony_ci instructionKey: "_instruction", 51e41f4b71Sopenharmony_ci attributesKey: "_attributes", 52e41f4b71Sopenharmony_ci textKey: "_text", 53e41f4b71Sopenharmony_ci cdataKey: "_cdata", 54e41f4b71Sopenharmony_ci doctypeKey: "_doctype", 55e41f4b71Sopenharmony_ci commentKey: "_comment", 56e41f4b71Sopenharmony_ci parentKey: "_parent", 57e41f4b71Sopenharmony_ci typeKey: "_type", 58e41f4b71Sopenharmony_ci nameKey: "_name", 59e41f4b71Sopenharmony_ci elementsKey: "_elements" 60e41f4b71Sopenharmony_ci } 61e41f4b71Sopenharmony_ci ``` 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci3. Call the conversion function and print the result. 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci ```ts 66e41f4b71Sopenharmony_ci let conv: convertxml.ConvertXML = new convertxml.ConvertXML(); 67e41f4b71Sopenharmony_ci let result: object = conv.convertToJSObject(xml, options); 68e41f4b71Sopenharmony_ci let strRes: string = JSON.stringify(result); // Convert the JavaScript object into a JSON string for explicit output. 69e41f4b71Sopenharmony_ci console.info(strRes); 70e41f4b71Sopenharmony_ci ``` 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci The output is as follows: 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci ```json 75e41f4b71Sopenharmony_ci strRes: 76e41f4b71Sopenharmony_ci {"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}},"_elements":[{"_type":"element","_name":"note", 77e41f4b71Sopenharmony_ci "_attributes":{"importance":"high","logged":"true"},"_elements":[{"_type":"element","_name":"title", 78e41f4b71Sopenharmony_ci "_elements":[{"_type":"text","_text":"Happy"}]},{"_type":"element","_name":"todo", 79e41f4b71Sopenharmony_ci "_elements":[{"_type":"text","_text":"Work"}]},{"_type":"element","_name":"todo", 80e41f4b71Sopenharmony_ci "_elements":[{"_type":"text","_text":"Play"}]}]}]} 81e41f4b71Sopenharmony_ci ``` 82