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 [@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 file into a JavaScript object to obtain the tag values, proceed as follows: 18 191. Import the **convertxml** module. 20 21 ```ts 22 import { convertxml } from '@kit.ArkTS'; 23 ``` 24 252. Enter the XML file 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 ```ts 28 let xml: string = 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: convertxml.ConvertOptions = { 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 ```ts 66 let conv: convertxml.ConvertXML = new convertxml.ConvertXML(); 67 let result: object = conv.convertToJSObject(xml, options); 68 let strRes: string = JSON.stringify(result); // Convert the JavaScript object into a JSON string for explicit output. 69 console.info(strRes); 70 ``` 71 72 The output is as follows: 73 74 ```json 75 strRes: 76 {"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}},"_elements":[{"_type":"element","_name":"note", 77 "_attributes":{"importance":"high","logged":"true"},"_elements":[{"_type":"element","_name":"title", 78 "_elements":[{"_type":"text","_text":"Happy"}]},{"_type":"element","_name":"todo", 79 "_elements":[{"_type":"text","_text":"Work"}]},{"_type":"element","_name":"todo", 80 "_elements":[{"_type":"text","_text":"Play"}]}]}]} 81 ``` 82