XML轉(zhuǎn)換

2024-02-16 13:48 更新

將XML文本轉(zhuǎn)換為JavaScript對象可以更輕松地處理和操作數(shù)據(jù),并且更適合在JavaScript應(yīng)用程序中使用。

語言基礎(chǔ)類庫提供ConvertXML類將XML文本轉(zhuǎn)換為JavaScript對象,輸入為待轉(zhuǎn)換的XML字符串及轉(zhuǎn)換選項,輸出為轉(zhuǎn)換后的JavaScript對象。具體轉(zhuǎn)換選項可見@ohos.convertxml。

注意事項

XML解析及轉(zhuǎn)換需要確保傳入的XML數(shù)據(jù)符合標(biāo)準(zhǔn)格式。

開發(fā)步驟

此處以XML轉(zhuǎn)為JavaScript對象后獲取其標(biāo)簽值為例,說明轉(zhuǎn)換效果。

  1. 引入模塊。

    1. import convertxml from '@ohos.convertxml';
  2. 輸入待轉(zhuǎn)換的XML,設(shè)置轉(zhuǎn)換選項。

    1. let xml =
    2. '<?xml version="1.0" encoding="utf-8"?>' +
    3. '<note importance="high" logged="true">' +
    4. ' <title>Happy</title>' +
    5. ' <todo>Work</todo>' +
    6. ' <todo>Play</todo>' +
    7. '</note>';
    8. let options = {
    9. // trim: false 轉(zhuǎn)換后是否刪除文本前后的空格,否
    10. // declarationKey: "_declaration" 轉(zhuǎn)換后文件聲明使用_declaration來標(biāo)識
    11. // instructionKey: "_instruction" 轉(zhuǎn)換后指令使用_instruction標(biāo)識
    12. // attributesKey: "_attributes" 轉(zhuǎn)換后屬性使用_attributes標(biāo)識
    13. // textKey: "_text" 轉(zhuǎn)換后標(biāo)簽值使用_text標(biāo)識
    14. // cdataKey: "_cdata" 轉(zhuǎn)換后未解析數(shù)據(jù)使用_cdata標(biāo)識
    15. // docTypeKey: "_doctype" 轉(zhuǎn)換后文檔類型使用_doctype標(biāo)識
    16. // commentKey: "_comment" 轉(zhuǎn)換后注釋使用_comment標(biāo)識
    17. // parentKey: "_parent" 轉(zhuǎn)換后父類使用_parent標(biāo)識
    18. // typeKey: "_type" 轉(zhuǎn)換后元素類型使用_type標(biāo)識
    19. // nameKey: "_name" 轉(zhuǎn)換后標(biāo)簽名稱使用_name標(biāo)識
    20. // elementsKey: "_elements" 轉(zhuǎn)換后元素使用_elements標(biāo)識
    21. trim: false,
    22. declarationKey: "_declaration",
    23. instructionKey: "_instruction",
    24. attributesKey: "_attributes",
    25. textKey: "_text",
    26. cdataKey: "_cdata",
    27. docTypeKey: "_doctype",
    28. commentKey: "_comment",
    29. parentKey: "_parent",
    30. typeKey: "_type",
    31. nameKey: "_name",
    32. elementsKey: "_elements"
    33. }
  3. 調(diào)用轉(zhuǎn)換函數(shù),打印結(jié)果。

    1. let conv = new convertxml.ConvertXML();
    2. let result = conv.convertToJSObject(xml, options);
    3. let strRes = JSON.stringify(result); // 將js對象轉(zhuǎn)換為json字符串,用于顯式輸出
    4. console.info(strRes);
    5. // 也可以直接處理轉(zhuǎn)換后的JS對象,獲取標(biāo)簽值
    6. let title = result['_elements'][0]['_elements'][0]['_elements'][0]['_text']; // 解析<title>標(biāo)簽對應(yīng)的值
    7. let todo = result['_elements'][0]['_elements'][1]['_elements'][0]['_text']; // 解析<todo>標(biāo)簽對應(yīng)的值
    8. let todo2 = result['_elements'][0]['_elements'][2]['_elements'][0]['_text']; // 解析<todo>標(biāo)簽對應(yīng)的值
    9. console.info(title); // Happy
    10. console.info(todo); // Work
    11. console.info(todo2); // Play

    輸出結(jié)果如下所示:

    1. strRes:
    2. {"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}},"_elements":[{"_type":"element","_name":"note",
    3. "_attributes":{"importance":"high","logged":"true"},"_elements":[{"_type":"element","_name":"title",
    4. "_elements":[{"_type":"text","_text":"Happy"}]},{"_type":"element","_name":"todo",
    5. "_elements":[{"_type":"text","_text":"Work"}]},{"_type":"element","_name":"todo",
    6. "_elements":[{"_type":"text","_text":"Play"}]}]}]}
    7. title:Happy
    8. todo:Work
    9. todo2:Play
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號