一、XML解析
對(duì)于以XML作為載體傳遞的數(shù)據(jù),實(shí)際使用中需要對(duì)相關(guān)的節(jié)點(diǎn)進(jìn)行解析,一般包括解析XML標(biāo)簽和標(biāo)簽值、解析XML屬性和屬性值、解析XML事件類型和元素深度三類場(chǎng)景。
XML模塊提供XmlPullParser類對(duì)XML文件解析,輸入為含有XML文本的ArrayBuffer或DataView,輸出為解析得到的信息。
表1 XML解析選項(xiàng)
名稱 | 類型 | 必填 | 說明 |
---|---|---|---|
supportDoctype | boolean | 否 | 是否忽略文檔類型。默認(rèn)為false,表示對(duì)文檔類型進(jìn)行解析。 |
ignoreNameSpace | boolean | 否 | 是否忽略命名空間。默認(rèn)為false,表示對(duì)命名空間進(jìn)行解析。 |
tagValueCallbackFunction | (name: string, value: string) => boolean | 否 | 獲取tagValue回調(diào)函數(shù),打印標(biāo)簽及標(biāo)簽值。默認(rèn)為null,表示不進(jìn)行XML標(biāo)簽和標(biāo)簽值的解析。 |
attributeValueCallbackFunction | (name: string, value: string) => boolean | 否 | 獲取attributeValue回調(diào)函數(shù), 打印屬性及屬性值。默認(rèn)為null,表示不進(jìn)行XML屬性和屬性值的解析。 |
tokenValueCallbackFunction | (eventType: EventType, value: ParseInfo) => boolean | 否 | 獲取tokenValue回調(diào)函數(shù),打印標(biāo)簽事件類型及parseInfo對(duì)應(yīng)屬性。默認(rèn)為null,表示不進(jìn)行XML事件類型解析。 |
注意事項(xiàng)
? ● XML解析及轉(zhuǎn)換需要確保傳入的XML數(shù)據(jù)符合標(biāo)準(zhǔn)格式。
? ● XML解析目前不支持按指定節(jié)點(diǎn)解析對(duì)應(yīng)的節(jié)點(diǎn)值。
解析XML標(biāo)簽和標(biāo)簽值
? 1. 引入模塊。
import xml from '@ohos.xml';
import util from '@ohos.util'; // 需要使用util模塊函數(shù)對(duì)文件編碼
2.XML文件編碼后調(diào)用XmlPullParser。
可以基于ArrayBuffer構(gòu)造XmlPullParser對(duì)象, 也可以基于DataView構(gòu)造XmlPullParser對(duì)象。
let strXml =
'<?xml version="1.0" encoding="utf-8"?>' +
'<note importance="high" logged="true">' +
'<title>Play</title>' +
'<lens>Work</lens>' +
'</note>';
let textEncoder = new util.TextEncoder();
let arrBuffer = textEncoder.encodeInto(strXml); // 對(duì)數(shù)據(jù)編碼,防止包含中文字符亂碼
// 1.基于ArrayBuffer構(gòu)造XmlPullParser對(duì)象
let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
// 2.基于DataView構(gòu)造XmlPullParser對(duì)象
let dataView = new DataView(arrBuffer.buffer);
let that = new xml.XmlPullParser(dataView, 'UTF-8');
? 3. 自定義回調(diào)函數(shù),本例直接打印出標(biāo)簽及標(biāo)簽值。
let str = '';
function func(name, value){
str = name + value;
console.info(str);
return true; //true:繼續(xù)解析 false:停止解析
}
? 4. 設(shè)置解析選項(xiàng),調(diào)用parse函數(shù)。
let options = {supportDoctype:true, ignoreNameSpace:true, tagValueCallbackFunction:func};
that.parse(options);
輸出結(jié)果如下所示:
note
title
Play
title
lens
Work
lens
note
解析XML屬性和屬性值
1.引入模塊。
import xml from '@ohos.xml';
import util from '@ohos.util'; // 需要使用util模塊函數(shù)對(duì)文件編碼
2.對(duì)XML文件編碼后調(diào)用XmlPullParser。
let strXml =
'<?xml version="1.0" encoding="utf-8"?>' +
'<note importance="high" logged="true">' +
' <title>Play</title>' +
' <title>Happy</title>' +
' <lens>Work</lens>' +
'</note>';
let textEncoder = new util.TextEncoder();
let arrBuffer = textEncoder.encodeInto(strXml); // 對(duì)數(shù)據(jù)編碼,防止包含中文字符亂碼
let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
3.自定義回調(diào)函數(shù),本例直接打印出屬性及屬性值。
let str = '';
function func(name, value){
str += name + ' ' + value + ' ';
return true; // true:繼續(xù)解析 false:停止解析
}
4.設(shè)置解析選項(xiàng),調(diào)用parse函數(shù)。
let options = {supportDoctype:true, ignoreNameSpace:true, attributeValueCallbackFunction:func};
that.parse(options);
console.info(str); // 一次打印出所有的屬性及其值
輸出結(jié)果如下所示:
importance high logged true // note節(jié)點(diǎn)的屬性及屬性值
解析XML事件類型和元素深度
? 1. 引入模塊。
import xml from '@ohos.xml';
import util from '@ohos.util'; // 需要使用util模塊函數(shù)對(duì)文件編碼
? 2. 對(duì)XML文件編碼后調(diào)用XmlPullParser。
let strXml =
'<?xml version="1.0" encoding="utf-8"?>' +
'<note importance="high" logged="true">' +
'<title>Play</title>' +
'</note>';
let textEncoder = new util.TextEncoder();
let arrBuffer = textEncoder.encodeInto(strXml); // 對(duì)數(shù)據(jù)編碼,防止包含中文字符亂碼
let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
? 3. 自定義回調(diào)函數(shù),本例直接打印元素事件類型及元素深度。
let str = '';
function func(name, value){
str = name + ' ' + value.getDepth(); // getDepth 獲取元素的當(dāng)前深度
console.info(str)
return true; //true:繼續(xù)解析 false:停止解析
}
? 4. 設(shè)置解析選項(xiàng),調(diào)用parse函數(shù)。
let options = {supportDoctype:true, ignoreNameSpace:true, tokenValueCallbackFunction:func};
that.parse(options);
輸出結(jié)果如下所示:
0 0 // 0:<?xml version="1.0" encoding="utf-8"?> 對(duì)應(yīng)事件類型START_DOCUMENT值為0 0:起始深度為0
2 1 // 2:<note importance="high" logged="true"> 對(duì)應(yīng)事件類型START_TAG值為2 1:深度為1
2 2 // 2:<title>對(duì)應(yīng)事件類型START_TAG值為2 2:深度為2
4 2 // 4:Play對(duì)應(yīng)事件類型TEXT值為4 2:深度為2
3 2 // 3:</title>對(duì)應(yīng)事件類型END_TAG值為3 2:深度為2
3 1 // 3:</note>對(duì)應(yīng)事件類型END_TAG值為3 1:深度為1(與<note對(duì)應(yīng)>)
1 0 // 1:對(duì)應(yīng)事件類型END_DOCUMENT值為1 0:深度為0
場(chǎng)景示例
場(chǎng)景示例
此處以調(diào)用所有解析選項(xiàng)為例,提供解析XML標(biāo)簽、屬性和事件類型的開發(fā)示例。
import xml from '@ohos.xml';
import util from '@ohos.util';
let strXml =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<book category="COOKING">' +
'<title lang="en">Everyday</title>' +
'<author>Giada</author>' +
'</book>';
let textEncoder = new util.TextEncoder();
let arrBuffer = textEncoder.encodeInto(strXml);
let that = new xml.XmlPullParser(arrBuffer.buffer, 'UTF-8');
let str = '';
function tagFunc(name, value) {
str = name + value;
console.info('tag-' + str);
return true;
}
function attFunc(name, value) {
str = name + ' ' + value;
console.info('attri-' + str);
return true;
}
function tokenFunc(name, value) {
str = name + ' ' + value.getDepth();
console.info('token-' + str);
return true;
}
let options = {
supportDocType: true,
ignoreNameSpace: true,
tagValueCallbackFunction: tagFunc,
attributeValueCallbackFunction: attFunc,
tokenValueCallbackFunction: tokenFunc
};
that.parse(options);
輸出結(jié)果如下所示:
tag-
token-0 0
tag-book
attri-category COOKING
token-2 1
tag-title
attri-lang en
token-2 2
tag-Everyday
token-4 2
tag-title
token-3 2
tag-author
token-2 2
tag-Giada
token-4 2
tag-author
token-3 2
tag-book
token-3 1
tag-
token-1 0
二、 XML轉(zhuǎn)換
將XML文本轉(zhuǎn)換為JavaScript對(duì)象可以更輕松地處理和操作數(shù)據(jù),并且更適合在JavaScript應(yīng)用程序中使用。
語言基礎(chǔ)類庫提供ConvertXML類將XML文本轉(zhuǎn)換為JavaScript對(duì)象,輸入為待轉(zhuǎn)換的XML字符串及轉(zhuǎn)換選項(xiàng),輸出為轉(zhuǎn)換后的JavaScript對(duì)象。具體轉(zhuǎn)換選項(xiàng)可見@ohos.convertxml。
注意事項(xiàng)
XML解析及轉(zhuǎn)換需要確保傳入的XML數(shù)據(jù)符合標(biāo)準(zhǔn)格式。
開發(fā)步驟
此處以XML轉(zhuǎn)為JavaScript對(duì)象后獲取其標(biāo)簽值為例,說明轉(zhuǎn)換效果。
? 1. 引入模塊。
import convertxml from '@ohos.convertxml';
? 2. 輸入待轉(zhuǎn)換的XML,設(shè)置轉(zhuǎn)換選項(xiàng)。
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)識(shí)
// instructionKey: "_instruction" 轉(zhuǎn)換后指令使用_instruction標(biāo)識(shí)
// attributesKey: "_attributes" 轉(zhuǎn)換后屬性使用_attributes標(biāo)識(shí)
// textKey: "_text" 轉(zhuǎn)換后標(biāo)簽值使用_text標(biāo)識(shí)
// cdataKey: "_cdata" 轉(zhuǎn)換后未解析數(shù)據(jù)使用_cdata標(biāo)識(shí)
// docTypeKey: "_doctype" 轉(zhuǎn)換后文檔類型使用_doctype標(biāo)識(shí)
// commentKey: "_comment" 轉(zhuǎn)換后注釋使用_comment標(biāo)識(shí)
// parentKey: "_parent" 轉(zhuǎn)換后父類使用_parent標(biāo)識(shí)
// typeKey: "_type" 轉(zhuǎn)換后元素類型使用_type標(biāo)識(shí)
// nameKey: "_name" 轉(zhuǎn)換后標(biāo)簽名稱使用_name標(biāo)識(shí)
// elementsKey: "_elements" 轉(zhuǎn)換后元素使用_elements標(biāo)識(shí)
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. 調(diào)用轉(zhuǎn)換函數(shù),打印結(jié)果。
let conv = new convertxml.ConvertXML();
let result = conv.convertToJSObject(xml, options);
let strRes = JSON.stringify(result); // 將js對(duì)象轉(zhuǎn)換為json字符串,用于顯式輸出
console.info(strRes);
// 也可以直接處理轉(zhuǎn)換后的JS對(duì)象,獲取標(biāo)簽值
let title = result['_elements'][0]['_elements'][0]['_elements'][0]['_text']; // 解析<title>標(biāo)簽對(duì)應(yīng)的值
let todo = result['_elements'][0]['_elements'][1]['_elements'][0]['_text']; // 解析<todo>標(biāo)簽對(duì)應(yīng)的值
let todo2 = result['_elements'][0]['_elements'][2]['_elements'][0]['_text']; // 解析<todo>標(biāo)簽對(duì)應(yīng)的值
console.info(title); // Happy
console.info(todo); // Work
console.info(todo2); // Play
輸出結(jié)果如下所示:文章來源:http://www.zghlxwxcb.cn/news/detail-710329.html
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
本文由博客一文多發(fā)平臺(tái) OpenWrite 發(fā)布!文章來源地址http://www.zghlxwxcb.cn/news/detail-710329.html
到了這里,關(guān)于【中秋國慶不斷更】XML在HarmonyOS中的生成,解析與轉(zhuǎn)換(下)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!