目录

How do I read the XML file in the rawfile subdirectory and convert its data to the string type?

Call getRawFileContent of resourceManager to obtain XML data. Call decodeToString of util to convert the XML data to the string type.

The code snippet is as follows:

import { BusinessError } from '@kit.BasicServicesKit';
import { util } from '@kit.ArkTS';

// In the utility class, retrieve the Context from the Entry Ability and save it to AppStore, then use AppStore to retrieve it in the utility class
let context = AppStorage.get("context") as UIContext;

try {
  context.getHostContext()!.resourceManager.getRawFileContent('test.xml', (error, value) => {
    if (error != null) {
      console.log('error is ' + error);
    } else {
      let rawFile = value;
      let textDecoder = util.TextDecoder.create('utf-8', { ignoreBOM : true });
      let rawFileString = textDecoder.decodeToString( rawFile , {stream: false});
    }
  });
} catch (error) {
  let code = (error as BusinessError).code;
  let message = (error as BusinessError).message;
  console.error(`callback getRawFileContent failed, error code: ${code}, message: ${message}.`);
}

Reference

getRawFileContent