docx
    Preparing search index...

    Class ImportedXmlComponent

    XML component representing imported XML content.

    ImportedXmlComponent allows you to parse XML strings and incorporate them into the document structure. This is particularly useful when working with templates or when you need to include pre-existing XML fragments.

    // Parse XML string into component tree
    const component = ImportedXmlComponent.fromXmlString(
    '<w:p><w:r><w:t>Hello World</w:t></w:r></w:p>'
    );

    // Manually create an imported component
    const element = new ImportedXmlComponent("w:customElement", { "w:val": "value" });
    element.push(new ImportedXmlComponent("w:child"));

    Hierarchy (View Summary)

    Index

    Constructors

    Methods

    • Prepares this component and its children for XML serialization.

      This method is called by the Formatter to convert the component tree into an object structure compatible with the xml library (https://www.npmjs.com/package/xml). It recursively processes all children and handles special cases like attribute-only elements and empty elements.

      The method can be overridden by subclasses to customize XML representation or execute side effects during serialization (e.g., creating relationships).

      Parameters

      • context: IContext

        The serialization context containing document state

      Returns IXmlableObject | undefined

      The XML-serializable object, or undefined to exclude from output

      // Override to add custom serialization logic
      prepForXml(context: IContext): IXmlableObject | undefined {
      // Custom logic here
      return super.prepForXml(context);
      }
    • Parses an XML string and converts it to an ImportedXmlComponent tree.

      This static method is the primary way to import external XML content. It uses xml-js to parse the XML string into a JSON representation, then converts that into a tree of XmlComponent objects.

      Parameters

      • importedContent: string

        The XML content as a string

      Returns ImportedXmlComponent

      An ImportedXmlComponent representing the parsed XML

      const xml = '<w:p><w:r><w:t>Hello</w:t></w:r></w:p>';
      const component = ImportedXmlComponent.fromXmlString(xml);