• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# XML转换
2<!--Kit: ArkTS-->
3<!--Subsystem: CommonLibrary-->
4<!--Owner: @xliu-huanwei; @shilei123; @huanghello-->
5<!--Designer: @yuanyao14-->
6<!--Tester: @kirl75; @zsw_zhushiwei-->
7<!--Adviser: @ge-yafang-->
8
9
10将XML文本转换为JavaScript对象,便于处理和操作数据,适用于JavaScript应用程序。
11
12
13语言基础类库提供ConvertXML类,将XML文本转换为JavaScript对象,输入为待转换的XML字符串及转换选项,输出为转换后的JavaScript对象。具体转换选项可见[API参考@ohos.convertxml](../reference/apis-arkts/js-apis-convertxml.md)。
14
15
16## 注意事项
17
18XML解析及转换需要确保传入的XML数据符合XML标准格式。
19
20
21## 开发步骤
22
23以XML转换为JavaScript对象为例,说明如何获取标签值。
24
251. 引入所需的模块。
26
27   ```ts
28   import { convertxml } from '@kit.ArkTS';
29   ```
30
312. 输入待转换的XML,设置转换选项。支持的转换选项及含义,请参见[ConvertOptions](../reference/apis-arkts/js-apis-convertxml.md#convertoptions)。
32
33   > **说明:**
34   >
35   > 请确保传入的XML文本符合标准格式,若包含“&”字符,请使用实体引用“\&amp;”替换。
36
37   ```ts
38   let xml: string =
39    '<?xml version="1.0" encoding="utf-8"?>' +
40    '<note importance="high" logged="true">' +
41    '    <title>Happy</title>' +
42    '    <todo>Work</todo>' +
43    '    <todo>Play</todo>' +
44    '</note>';
45   let options: convertxml.ConvertOptions = {
46     // trim: false 转换后是否删除文本前后的空格,否
47     // declarationKey: "_declaration" 转换后文件声明使用_declaration来标识
48     // instructionKey: "_instruction" 转换后指令使用_instruction标识
49     // attributesKey: "_attributes" 转换后属性使用_attributes标识
50     // textKey: "_text" 转换后标签值使用_text标识
51     // cdataKey: "_cdata" 转换后未解析数据使用_cdata标识
52     // docTypeKey: "_doctype" 转换后文档类型使用_doctype标识
53     // commentKey: "_comment" 转换后注释使用_comment标识
54     // parentKey: "_parent" 转换后父类使用_parent标识
55     // typeKey: "_type" 转换后元素类型使用_type标识
56     // nameKey: "_name" 转换后标签名称使用_name标识
57     // elementsKey: "_elements" 转换后元素使用_elements标识
58     trim: false,
59     declarationKey: "_declaration",
60     instructionKey: "_instruction",
61     attributesKey: "_attributes",
62     textKey: "_text",
63     cdataKey: "_cdata",
64     doctypeKey: "_doctype",
65     commentKey: "_comment",
66     parentKey: "_parent",
67     typeKey: "_type",
68     nameKey: "_name",
69     elementsKey: "_elements"
70   }
71   ```
72
733. 调用fastConvertToJSObject函数并打印结果。
74
75   ```ts
76   let conv: convertxml.ConvertXML = new convertxml.ConvertXML();
77   let result: object = conv.fastConvertToJSObject(xml, options);
78   let strRes: string = JSON.stringify(result); // 将js对象转换为json字符串,用于显式输出
79   console.info(strRes);
80   ```
81
82   输出结果如下所示:
83
84   ```json
85   strRes:
86   {"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}},"_elements":[{"_type":"element","_name":"note",
87    "_attributes":{"importance":"high","logged":"true"},"_elements":[{"_type":"element","_name":"title","_parent":"note",
88    "_elements":[{"_type":"text","_text":"Happy"}]},{"_type":"element","_name":"todo","_parent":"note","_elements":
89    [{"_type":"text","_text":"Work"}]},{"_type":"element","_name":"todo","_parent":"note","_elements":[{"_type":"text",
90    "_text":"Play"}]}]}]}
91   ```
92