openharmony 鸿蒙 js-apis-convertxml

2025-06-12 浏览 (1)

@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 '@kit.ArkTS';

ConvertXML

fastConvertToJSObject14+

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

Converts an XML text into a JavaScript object.

NOTE

In Windows, a newline is usually represented by the carriage return (CR) followed by the line feed (LF). However, the object obtained by calling this API uses only the LF to indicate a new line.

Atomic service API: This API can be used in atomic services since API version 14.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
xmlstringYesXML text to convert. If the XML text contains the ampersand (&), replace it with the entity reference &.
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 Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
10200002Invalid xml string.

Example

try {
  let xml =
    '<?xml version="1.0" encoding="utf-8"?>' +
    '<note importance="high" logged="true">' +
    '   <title>Hello\r\nWorld</title>' +
    '   <todo><![CDATA[Work\r\n]]></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.fastConvertToJSObject(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":"Hello\nWorld"}]},{"_type":"element","_name":"todo","_elements":[{"_type":"cdata","_cdata":"Work\n"}]}]}]}

convertToJSObject(deprecated)

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

Converts an XML text into a JavaScript object.

NOTE

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

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
xmlstringYesXML text to convert. If the XML text contains the ampersand (&), replace it with the entity reference &amp;.
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 Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
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 fastConvertToJSObject14+ 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.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Utils.Lang

NameTypeMandatoryDescription
trimbooleanYesWhether to trim the whitespace characters before and after the text. The value true means to trim the whitespace characters before and after the text, and false means to keep them.
ignoreDeclarationbooleanNoWhether to ignore the XML declaration. The value true means to ignore the XML declaration, and false means the opposite. The default value is false.
ignoreInstructionbooleanNoWhether to ignore the XML processing instruction. The value true means to ignore the XML processing instruction, and false means the opposite. The default value is false.
ignoreAttributesbooleanNoWhether to ignore the element's attribute information. The value true means to ignore the element's attribute information, and false means the opposite. The default value is false.
ignoreCommentbooleanNoWhether to ignore element comments. The value true means to ignore element comments, and false means the opposite. The default value is false.
ignoreCDATAbooleanNoWhether to ignore the element's CDATA information. The value true means to ignore the element's CDATA information, and false means the opposite. The default value is false.
ignoreDoctypebooleanNoWhether to ignore the element's Doctype information. The value true means to ignore the element's Doctype information, and false means the opposite. The default value is false.
ignoreTextbooleanNoWhether to ignore the element's text information. The value true means to ignore the element's text information, and false means the opposite. The default value is false.
declarationKeystringYesName of the attribute key for declaration in the output object.
instructionKeystringYesName of the attribute key for instruction in the output object.
attributesKeystringYesName of the attribute key for attributes in the output object.
textKeystringYesName of the attribute key for text in the output object.
cdataKeystringYesName of the attribute key for cdata in the output object.
doctypeKeystringYesName of the attribute key for doctype in the output object.
commentKeystringYesName of the attribute key for comment in the output object.
parentKeystringYesName of the attribute key for parent in the output object.
typeKeystringYesName of the attribute key for type in the output object.
nameKeystringYesName of the attribute key for name in the output object.
elementsKeystringYesName of the attribute key for elements in the output object.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkTS

harmony 鸿蒙Compilation Toolchain Error Codes

harmony 鸿蒙TypeScript Compiler Error Codes

harmony 鸿蒙Utils Error Codes

harmony 鸿蒙js-apis-arkts-collections

harmony 鸿蒙@arkts.math.Decimal (High-Precision Math Library Decimal)

harmony 鸿蒙@arkts.lang (ArkTS Base Capability)

harmony 鸿蒙@arkts.utils (ArkTS Utils)

harmony 鸿蒙@ohos.util.ArrayList (Linear Container ArrayList)

harmony 鸿蒙@ohos.buffer (Buffer)

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