@ohos.convertxml (xml轉(zhuǎn)換JavaScript)
本模塊提供轉(zhuǎn)換xml文本為JavaScript對象的功能。
說明:
導(dǎo)入模塊
import convertxml from '@ohos.convertxml';
ConvertXML
convertToJSObject9+
convertToJSObject(xml: string, options?: ConvertOptions) : Object
轉(zhuǎn)換xml文本為JavaScript對象。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
| 參數(shù)名 | 類型 | 必填 | 說明 |
|---|---|---|---|
| xml | string | 是 | 傳入的xml文本。 |
| options | [ConvertOptions] | 否 | 轉(zhuǎn)換選項 , 默認值是ConvertOptions對象 , 由其中各個屬性的默認值組成。 |
返回值:
| 類型 | 說明 |
|---|---|
| Object | 處理后返回的JavaScript對象。 |
錯誤碼:
以下錯誤碼的詳細介紹請參見[語言基礎(chǔ)類庫錯誤碼]
| 錯誤碼ID | 錯誤信息 |
|---|---|
| 10200002 | Invalid xml string. |
示例:
try {
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 conv = new convertxml.ConvertXML()
let options = {
trim: false, declarationKey: "_declaration",
instructionKey: "_instruction", attributesKey: "_attributes",
textKey: "_text", cdataKey: "_cdata", doctypeKey: "_doctype",
commentKey: "_comment", parentKey: "_parent", typeKey: "_type",
nameKey: "_name", elementsKey: "_elements"
}
let result = JSON.stringify(conv.convertToJSObject(xml, options));
console.log(result);
} catch (e) {
console.log(e.toString());
}
// 輸出(寬泛型)
// {"_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"}]}]}]}
convert(deprecated)
convert(xml: string, options?: ConvertOptions) : Object
轉(zhuǎn)換xml文本為JavaScript對象。
說明:
從API version 8開始支持,從API version 9開始廢棄,建議使用[convertToJSObject9+]替代。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
| 參數(shù)名 | 類型 | 必填 | 說明 |
|---|---|---|---|
| xml | string | 是 | 傳入的xml文本。 |
| options | [ConvertOptions] | 否 | 轉(zhuǎn)換選項 , 默認值是ConvertOptions對象 , 由其中各個屬性的默認值組成。 |
返回值:
| 類型 | 說明 |
|---|---|
| Object | 處理后返回的JavaScript對象。 |
示例:
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 conv = new convertxml.ConvertXML();
let options = {trim : false, declarationKey:"_declaration",
instructionKey : "_instruction", attributesKey : "_attributes",
textKey : "_text", cdataKey:"_cdata", doctypeKey : "_doctype",
commentKey : "_comment", parentKey : "_parent", typeKey : "_type",
nameKey : "_name", elementsKey : "_elements"}
let result = JSON.stringify(conv.convert(xml, options));
console.log(result);
// 輸出(寬泛型)
// {"_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"}]}]}]}
ConvertOptions
鴻蒙開發(fā)指導(dǎo)文檔:[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]

轉(zhuǎn)換選項。
系統(tǒng)能力: SystemCapability.Utils.Lang
| 名稱 | 類型 | 必填 | 說明 |
|---|---|---|---|
| trim | boolean | 是 | 是否修剪位于文本前后的空白字符。 |
| ignoreDeclaration | boolean | 否 | 是否忽略xml寫入聲明指示,默認false。 |
| ignoreInstruction | boolean | 否 | 是否忽略xml的寫入處理指令,默認false。 |
| ignoreAttributes | boolean | 否 | 是否跨多行打印屬性并縮進屬性,默認false。 |
| ignoreComment | boolean | 否 | 是否忽略元素的注釋信息,默認false。 |
| ignoreCDATA | boolean | 否 | 是否忽略元素的CDATA信息,默認false。 |
| ignoreDoctype | boolean | 否 | 是否忽略元素的Doctype信息,默認false。 |
| ignoreText | boolean | 否 | 是否忽略元素的文本信息,默認false。 |
| declarationKey | string | 是 | 用于輸出對象中declaration的屬性鍵的名稱。 |
| instructionKey | string | 是 | 用于輸出對象中instruction的屬性鍵的名稱。 |
| attributesKey | string | 是 | 用于輸出對象中attributes的屬性鍵的名稱。 |
| textKey | string | 是 | 用于輸出對象中text的屬性鍵的名稱。 |
| cdataKey | string | 是 | 用于輸出對象中cdata的屬性鍵的名稱 |
| doctypeKey | string | 是 | 用于輸出對象中doctype的屬性鍵的名稱。 |
| commentKey | string | 是 | 用于輸出對象中comment的屬性鍵的名稱。 |
| parentKey | string | 是 | 用于輸出對象中parent的屬性鍵的名稱。 |
| typeKey | string | 是 | 用于輸出對象中type的屬性鍵的名稱。 |
| nameKey | string | 是 | 用于輸出對象中name的屬性鍵的名稱。 |
| elementsKey | string | 是 | 用于輸出對象中elements的屬性鍵的名稱。 |
審核編輯 黃宇
-
鴻蒙
+關(guān)注
關(guān)注
60文章
2963瀏覽量
45908 -
OpenHarmony
+關(guān)注
關(guān)注
33文章
3952瀏覽量
21106
發(fā)布評論請先 登錄
趕緊收藏!7大類400多種組件,鴻蒙三方庫來了!
HarmonyOS/OpenHarmony應(yīng)用開發(fā)-Stage模型ArkTS語言擴展能力基類
OpenHarmony C++公共基礎(chǔ)類庫應(yīng)用案例:HelloWorld
OpenHarmony C++公共基礎(chǔ)類庫應(yīng)用案例:Thread
基于openharmony移植的ohos拼圖支持庫
基于openharmony適配移植的Ohos應(yīng)用程序評價
openharmony第三方組件適配移植易于使用的ohos庫
基于openharmony適配移植的控件和常用類庫組合成的Genius-Ohos
HarmonyOS語言基礎(chǔ)類庫開發(fā)指南上線啦!
OpenHarmony C++公共基礎(chǔ)類庫應(yīng)用案例:Thread
OpenHarmony C++公共基礎(chǔ)類庫應(yīng)用案例:HelloWorld
OpenHarmony標準系統(tǒng)C++公共基礎(chǔ)類庫案例:HelloWorld
基于OpenHarmony標準系統(tǒng)的C++公共基礎(chǔ)類庫案例:SafeBlockQueue
OpenHarmony語言基礎(chǔ)類庫【@ohos.convertxml】
評論