harmony 鸿蒙XML Conversion
XML Conversion
Converting XML text into JavaScript objects can simplify data handling and manipulation, making it more suitable for use in JavaScript applications.
The 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).
Precautions
To ensure successful XML parsing and conversion, the input XML data must comply with the standard format.
How to Develop
To convert an XML document into a JavaScript object and obtain the tag values, proceed as follows:
- Import the module.
import { convertxml } from '@kit.ArkTS';
- Input the XML document to be converted and set conversion options. For details about the supported conversion options and their meanings, see ConvertOptions.
NOTE
If the XML text to convert contains the ampersand (&), replace it with the entity reference &.
let xml: string =
'<?xml version="1.0" encoding="utf-8"?>' +
'<note importance="high" logged="true">' +
' <title>Happy</title>' +
' <todo>Work</todo>' +
' <todo>Play</todo>' +
'</note>';
let options: convertxml.ConvertOptions = {
// trim: false, indicating that spaces before and after the text are not deleted after conversion.
// declarationKey: "_declaration", indicating that _declaration is used to identify the file declaration after conversion.
// instructionKey: "_instruction", indicating that _instruction is used to identify instructions after conversion.
// attributesKey: "_attributes", indicating that _attributes is used to identify attributes after conversion.
// textKey: "_text", indicating that _text is used to identify tag values after conversion.
// cdataKey: "_cdata", indicating that _cdata is used to identify unparsed data after conversion.
// docTypeKey: "_doctype", indicating that _doctype is used to identify documents after conversion.
// commentKey: "_comment", indicating that _comment is used to identify comments after conversion.
// parentKey: "_parent", indicating that _parent is used to identify parent classes after conversion.
// typeKey: "_type", indicating that _type is used to identify types after conversion.
// nameKey: "_name", indicating that _name is used to identify tag names after conversion.
// elementsKey: "_elements", indicating that _elements is used to identify elements after conversion.
trim: false,
declarationKey: "_declaration",
instructionKey: "_instruction",
attributesKey: "_attributes",
textKey: "_text",
cdataKey: "_cdata",
doctypeKey: "_doctype",
commentKey: "_comment",
parentKey: "_parent",
typeKey: "_type",
nameKey: "_name",
elementsKey: "_elements"
}
- Call the conversion function and print the result.
let conv: convertxml.ConvertXML = new convertxml.ConvertXML();
let result: object = conv.convertToJSObject(xml, options);
let strRes: string = JSON.stringify(result); // Convert the JavaScript object into a JSON string for explicit output.
console.info(strRes);
The output is as follows:
strRes:
{"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}},"_elements":[{"_type":"element","_name":"note",
"_attributes":{"importance":"high","logged":"true"},"_elements":[{"_type":"element","_name":"title",
"_elements":[{"_type":"text","_text":"Happy"}]},{"_type":"element","_name":"todo",
"_elements":[{"_type":"text","_text":"Work"}]},{"_type":"element","_name":"todo",
"_elements":[{"_type":"text","_text":"Play"}]}]}]}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Configuring arkOptions in build-profile.json5
harmony 鸿蒙Ark Bytecode File Format
harmony 鸿蒙Naming Conventions for Ark Bytecode Functions
harmony 鸿蒙Ark Bytecode Fundamentals
harmony 鸿蒙Overview of Ark Bytecode
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦