xml轉(zhuǎn)換JavaScript

2024-01-23 18:06 更新

本模塊提供轉(zhuǎn)換xml文本為JavaScript對(duì)象的功能。

說明

本模塊首批接口從API version 8開始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。

導(dǎo)入模塊

  1. import convertxml from '@ohos.convertxml';

ConvertXML

convertToJSObject9+

convertToJSObject(xml: string, options?: ConvertOptions) : Object

轉(zhuǎn)換xml文本為JavaScript對(duì)象。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數(shù):

參數(shù)名

類型

必填

說明

xml

string

傳入的xml文本。

options

ConvertOptions

轉(zhuǎn)換選項(xiàng),默認(rèn)值是ConvertOptions對(duì)象 , 由其中各個(gè)屬性的默認(rèn)值組成。。

返回值:

類型

說明

Object

處理后返回的JavaScript對(duì)象。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語言基礎(chǔ)類庫錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

10200002

Invalid xml string.

示例:

  1. try {
  2. let xml =
  3. '<?xml version="1.0" encoding="utf-8"?>' +
  4. '<note importance="high" logged="true">' +
  5. ' <title>Happy</title>' +
  6. ' <todo>Work</todo>' +
  7. ' <todo>Play</todo>' +
  8. '</note>';
  9. let conv = new convertxml.ConvertXML()
  10. let options = {
  11. trim: false, declarationKey: "_declaration",
  12. instructionKey: "_instruction", attributesKey: "_attributes",
  13. textKey: "_text", cdataKey: "_cdata", doctypeKey: "_doctype",
  14. commentKey: "_comment", parentKey: "_parent", typeKey: "_type",
  15. nameKey: "_name", elementsKey: "_elements"
  16. }
  17. let result = JSON.stringify(conv.convertToJSObject(xml, options));
  18. console.log(result);
  19. } catch (e) {
  20. console.log(e.toString());
  21. }
  22. // 輸出(寬泛型)
  23. // {"_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對(duì)象。

說明

從API version 8開始支持,從API version 9開始廢棄,建議使用convertToJSObject9+替代。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數(shù):

參數(shù)名

類型

必填

說明

xml

string

傳入的xml文本。

options

ConvertOptions

轉(zhuǎn)換選項(xiàng),默認(rèn)值是ConvertOptions對(duì)象 , 由其中各個(gè)屬性的默認(rèn)值組成。。

返回值:

類型

說明

Object

處理后返回的JavaScript對(duì)象。

示例:

  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 conv = new convertxml.ConvertXML();
  9. let options = {trim : false, declarationKey:"_declaration",
  10. instructionKey : "_instruction", attributesKey : "_attributes",
  11. textKey : "_text", cdataKey:"_cdata", doctypeKey : "_doctype",
  12. commentKey : "_comment", parentKey : "_parent", typeKey : "_type",
  13. nameKey : "_name", elementsKey : "_elements"}
  14. let result = JSON.stringify(conv.convert(xml, options));
  15. console.log(result);
  16. // 輸出(寬泛型)
  17. // {"_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

轉(zhuǎn)換選項(xiàng)。

系統(tǒng)能力: SystemCapability.Utils.Lang

名稱

類型

必填

說明

trim

boolean

是否修剪位于文本前后的空白字符。

ignoreDeclaration

boolean

是否忽略xml寫入聲明指示,默認(rèn)false。

ignoreInstruction

boolean

是否忽略xml的寫入處理指令,默認(rèn)false。

ignoreAttributes

boolean

是否跨多行打印屬性并縮進(jìn)屬性,默認(rèn)false。

ignoreComment

boolean

是否忽略元素的注釋信息,默認(rèn)false。

ignoreCDATA

boolean

是否忽略元素的CDATA信息,默認(rèn)false。

ignoreDoctype

boolean

是否忽略元素的Doctype信息,默認(rèn)false。

ignoreText

boolean

是否忽略元素的文本信息,默認(rèn)false。

declarationKey

string

用于輸出對(duì)象中declaration的屬性鍵的名稱。

instructionKey

string

用于輸出對(duì)象中instruction的屬性鍵的名稱。

attributesKey

string

用于輸出對(duì)象中attributes的屬性鍵的名稱。

textKey

string

用于輸出對(duì)象中text的屬性鍵的名稱。

cdataKey

string

用于輸出對(duì)象中cdata的屬性鍵的名稱。

doctypeKey

string

用于輸出對(duì)象中doctype的屬性鍵的名稱。

commentKey

string

用于輸出對(duì)象中comment的屬性鍵的名稱。

parentKey

string

用于輸出對(duì)象中parent的屬性鍵的名稱。

typeKey

string

用于輸出對(duì)象中type的屬性鍵的名稱。

nameKey

string

用于輸出對(duì)象中name的屬性鍵的名稱。

elementsKey

string

用于輸出對(duì)象中elements的屬性鍵的名稱。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)