xml2js#OptionsV2 TypeScript Examples

The following examples show how to use xml2js#OptionsV2. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: xml-read.ts    From attranslate with MIT License 6 votes vote down vote up
export async function parseRawXML<T>(
  xmlString: string,
  args: ReadTFileArgs
): Promise<T> {
  try {
    const options: OptionsV2 = {
      ...sharedXmlOptions,
      strict: true,
      async: false,
      //explicitChildren: true, // if true, then the resulting object will be entirely different
      preserveChildrenOrder: true,
      headless: true,
      //emptyTag: " ",
      includeWhiteChars: true,
      trim: false,
      normalize: false,
      normalizeTags: false,
    };
    // eslint-disable-next-line @typescript-eslint/no-var-requires
    const xml2js = require("xml2js");
    const result = await xml2js.parseStringPromise(xmlString, options);
    return result as T;
  } catch (e) {
    console.error(e);
    logParseError("XML parsing error", args);
  }
}
Example #2
Source File: xml-generic.ts    From attranslate with MIT License 5 votes vote down vote up
sharedXmlOptions: OptionsV2 = {
  attrkey: "attributesObj",
  charkey: "characterContent",
}
Example #3
Source File: xml-write.ts    From attranslate with MIT License 5 votes vote down vote up
export function writeXmlResourceFile(
  xmlFile: XmlFile,
  args: WriteTFileArgs,
  auxData: XmlAuxData | null
) {
  // eslint-disable-next-line @typescript-eslint/no-var-requires
  const xml2js = require("xml2js");
  const stringIndent = " ".repeat(
    auxData?.detectedIntent ?? DEFAULT_XML_INDENT
  );
  const options: OptionsV2 = {
    ...sharedXmlOptions,
    headless: true,
    renderOpts: {
      pretty: true,
      indent: stringIndent,
    },
  };
  // See https://github.com/oozcitak/xmlbuilder-js/wiki/Builder-Options for available xmlBuilderOptions
  const xmlBuilderOptions = {
    noValidation: false,
    noDoubleEncoding: true,
  };
  const mergedOptions = { ...options, xmlBuilderOptions };
  const builder: Builder = new xml2js.Builder(mergedOptions);
  const rawXmlString: string = builder.buildObject(xmlFile);
  let xmlHeader: string = DEFAULT_XML_HEADER + "\n";
  if (auxData) {
    if (auxData.xmlHeader) {
      xmlHeader = auxData.xmlHeader + "\n";
    } else {
      xmlHeader = "";
    }
  }
  const xmlString = `${xmlHeader}${removeBlankLines(rawXmlString)}\n`;
  writeManagedUtf8({ path: args.path, utf8: xmlString });
}