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
    • get writtenAs(): readonly BaseXmlComponent[] | undefined
      Internal

      A component of no element, such as the one fromXmlString gives for the parsed document, is written as the elements in it. Written as itself, it would be an <undefined> element, which Word can't open.

      The component itself is still returned, rather than its element, because code written for earlier versions takes the element from its children.

      Returns readonly BaseXmlComponent[] | undefined

    • Parses an XML string and converts it to an ImportedXmlComponent tree.

      This static method is the primary way to import external XML content. It parses the XML string into an XmlElement tree, then converts that into a tree of XmlComponent objects.

      The component is for the parsed document, with the XML's element in it, and is written as that element, with no element of its own around it.

      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);