xml-conversion.md 3.5 KB
Newer Older
G
ge-yafang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
# XML转换


将XML文本转换为JavaScript对象可以更轻松地处理和操作数据,并且更适合在JavaScript应用程序中使用。


语言基础类库提供ConvertXML类将xml文本转换为JavaScript对象,输入为待转换的XML字符串及转换选项,输出为转换后的JavaScript对象。具体转换选项可见[API参考@ohos.convertxml](../reference/apis/js-apis-convertxml.md)


## 注意事项

XML解析及转换需要确认传入的XML数据符合标准格式。


## 开发步骤

此处以XML转为JavaScript对象后获取其标签值为例,说明转换效果。

1. 引入模块。
   
   ```js
   import convertxml from '@ohos.convertxml';
   ```

2. 输入待转换XML,设置转换选项。
   
   ```js
   let xml =
     '<?xml version="1.0" encoding="utf-8"?>' +
       '<note importance="high" logged="true">' +
       '    <title>Happy</title>' +
       '    <todo>Work</todo>' +
       '    <todo>Play</todo>' +
       '</note>';
   let options = {
     // trim: false 转换后是否删除文本前后的空格,否
     // declarationKey: "_declaration" 转换后文件声明使用_declaration来标识
     // instructionKey: "_instruction" 转换后指令使用_instruction标识
     // attributesKey: "_attributes" 转换后属性使用_attributes标识
     // textKey: "_text" 转换后标签值使用_text标识
     // cdataKey: "_cdata" 转换后未解析数据使用_cdata标识
     // docTypeKey: "_doctype" 转换后文档类型使用_doctype标识
     // commentKey: "_comment" 转换后注释使用_comment标识
     // parentKey: "_parent" 转换后父类使用_parent标识
     // typeKey: "_type" 转换后元素类型使用_type标识
     // nameKey: "_name" 转换后标签名称使用_name标识
     // elementsKey: "_elements" 转换后元素使用_elements标识
     trim: false,
     declarationKey: "_declaration",
     instructionKey: "_instruction",
     attributesKey: "_attributes",
     textKey: "_text",
     cdataKey: "_cdata",
     docTypeKey: "_doctype",
     commentKey: "_comment",
     parentKey: "_parent",
     typeKey: "_type",
     nameKey: "_name",
     elementsKey: "_elements"
   }
   ```

3. 调用转换函数,打印结果。
   
   ```js
   let conv = new convertxml.ConvertXML();
   let result = conv.convertToJSObject(xml, options);
   let strRes = JSON.stringify(result); // 将js对象转换为json字符串,用于显式输出
   console.info(strRes);
   // 也可以直接处理转换后的JS对象,获取标签值
   let title = result['_elements'][0]['_elements'][0]['_elements'][0]['_text']; // 解析<title>标签对应的值
   let todo = result['_elements'][0]['_elements'][1]['_elements'][0]['_text']; // 解析<todo>标签对应的值
   let todo2 = result['_elements'][0]['_elements'][2]['_elements'][0]['_text']; // 解析<todo>标签对应的值
   console.info(title); // Happy
   console.info(todo); // Work
   console.info(todo2); // Play
   ```

   输出结果如下所示:

   
   ```js
   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"}]}]}]}
   title:Happy
   todo:Work
   todo2:Play
   ```