harmony 鸿蒙@ohos.convertxml (XML-to-JavaScript Conversion)

2022-08-09 浏览 (1181)

@ohos.convertxml (XML-to-JavaScript Conversion)

The convertxml module provides APIs for converting XML text into JavaScript objects.

NOTE

The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import convertxml from '@ohos.convertxml';

ConvertXML

convertToJSObject9+

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

Converts an XML text into a JavaScript object.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
xmlstringYesXML text to convert.
optionsConvertOptionsNoOptions for conversion. The default value is a ConvertOptions object, which consists of the default values of the attributes in the object.

Return value

TypeDescription
ObjectJavaScript object.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200002Invalid xml string.

Example

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: convertxml.ConvertOptions = {
    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 as Object).toString());
}
// Output (non-compact)
// {"_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

Converts an XML text into a JavaScript object.

NOTE

This API is supported since API version 8 and deprecated since API version 9. You are advised to use convertToJSObject9+ instead.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
xmlstringYesXML text to convert.
optionsConvertOptionsNoOptions for conversion. The default value is a ConvertOptions object, which consists of the default values of the attributes in the object.

Return value

TypeDescription
ObjectJavaScript object.

Example

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: convertxml.ConvertOptions = {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);
// Output (non-compact)
// {"_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

Options for conversion.

System capability: SystemCapability.Utils.Lang

NameTypeMandatoryDescription
trimbooleanYesWhether to trim the whitespace characters before and after the text. The default value is false.
ignoreDeclarationbooleanNoWhether to ignore the XML declaration. The default value is false.
ignoreInstructionbooleanNoWhether to ignore the XML processing instruction. The default value is false.
ignoreAttributesbooleanNoWhether to print attributes across multiple lines and indent attributes. The default value is false.
ignoreCommentbooleanNoWhether to ignore element comments. The default value is false.
ignoreCDATAbooleanNoWhether to ignore the element's CDATA information. The default value is false.
ignoreDoctypebooleanNoWhether to ignore the element's Doctype information. The default value is false.
ignoreTextbooleanNoWhether to ignore the element's text information. The default value is false.
declarationKeystringYesName of the attribute key for declaration in the output object. The default value is _declaration.
instructionKeystringYesName of the attribute key for instruction in the output object. The default value is _instruction.
attributesKeystringYesName of the attribute key for attributes in the output object. The default value is _attributes.
textKeystringYesName of the attribute key for text in the output object. The default value is _text.
cdataKeystringYesName of the attribute key for CDATA in the output object. The default value is _cdata.
doctypeKeystringYesName of the attribute key for Doctype in the output object. The default value is _doctype.
commentKeystringYesName of the attribute key for comment in the output object. The default value is _comment.
parentKeystringYesName of the attribute key for parent in the output object. The default value is _parent.
typeKeystringYesName of the attribute key for type in the output object. The default value is _type.
nameKeystringYesName of the attribute key for name in the output object. The default value is _name.
elementsKeystringYesName of the attribute key for elements in the output object. The default value is _elements.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/1ee5b723d124487caa43f1e7e77a2233