W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
將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)格式。
此處以XML轉(zhuǎn)為JavaScript對象后獲取其標(biāo)簽值為例,說明轉(zhuǎn)換效果。
引入模塊。
- import convertxml from '@ohos.convertxml';
輸入待轉(zhuǎn)換的XML,設(shè)置轉(zhuǎn)換選項。
- 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 轉(zhuǎn)換后是否刪除文本前后的空格,否
- // declarationKey: "_declaration" 轉(zhuǎn)換后文件聲明使用_declaration來標(biāo)識
- // instructionKey: "_instruction" 轉(zhuǎn)換后指令使用_instruction標(biāo)識
- // attributesKey: "_attributes" 轉(zhuǎn)換后屬性使用_attributes標(biāo)識
- // textKey: "_text" 轉(zhuǎn)換后標(biāo)簽值使用_text標(biāo)識
- // cdataKey: "_cdata" 轉(zhuǎn)換后未解析數(shù)據(jù)使用_cdata標(biāo)識
- // docTypeKey: "_doctype" 轉(zhuǎn)換后文檔類型使用_doctype標(biāo)識
- // commentKey: "_comment" 轉(zhuǎn)換后注釋使用_comment標(biāo)識
- // parentKey: "_parent" 轉(zhuǎn)換后父類使用_parent標(biāo)識
- // typeKey: "_type" 轉(zhuǎn)換后元素類型使用_type標(biāo)識
- // nameKey: "_name" 轉(zhuǎn)換后標(biāo)簽名稱使用_name標(biāo)識
- // elementsKey: "_elements" 轉(zhuǎn)換后元素使用_elements標(biāo)識
- trim: false,
- declarationKey: "_declaration",
- instructionKey: "_instruction",
- attributesKey: "_attributes",
- textKey: "_text",
- cdataKey: "_cdata",
- docTypeKey: "_doctype",
- commentKey: "_comment",
- parentKey: "_parent",
- typeKey: "_type",
- nameKey: "_name",
- elementsKey: "_elements"
- }
調(diào)用轉(zhuǎn)換函數(shù),打印結(jié)果。
- let conv = new convertxml.ConvertXML();
- let result = conv.convertToJSObject(xml, options);
- let strRes = JSON.stringify(result); // 將js對象轉(zhuǎn)換為json字符串,用于顯式輸出
- console.info(strRes);
- // 也可以直接處理轉(zhuǎn)換后的JS對象,獲取標(biāo)簽值
- let title = result['_elements'][0]['_elements'][0]['_elements'][0]['_text']; // 解析<title>標(biāo)簽對應(yīng)的值
- let todo = result['_elements'][0]['_elements'][1]['_elements'][0]['_text']; // 解析<todo>標(biāo)簽對應(yīng)的值
- let todo2 = result['_elements'][0]['_elements'][2]['_elements'][0]['_text']; // 解析<todo>標(biāo)簽對應(yīng)的值
- console.info(title); // Happy
- console.info(todo); // Work
- console.info(todo2); // Play
輸出結(jié)果如下所示:
- 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
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: