Class XMLDocumentGenerator

java.lang.Object
org.eclipse.lemminx.extensions.contentmodel.generator.XMLDocumentGenerator

public class XMLDocumentGenerator extends Object
Generates a complete XML document from a grammar (XSD, DTD, RelaxNG, RNC).

This generator creates a well-formed XML document that includes:

  • XML declaration (<?xml version="1.0" encoding="UTF-8"?>)
  • Grammar binding depending on the grammar type:
    • XSD: xsi:schemaLocation or xsi:noNamespaceSchemaLocation
    • DTD: <!DOCTYPE root SYSTEM "uri">
    • RelaxNG/RNC: <?xml-model href="uri"?>
  • XML body with required elements and attributes generated via XMLElementGenerator

Example usage:

 XMLDocumentGenerator generator = new XMLDocumentGenerator(sharedSettings);
 String xml = generator.generate(cmDocument, grammarURI, "rootElement");
 
  • Constructor Details

    • XMLDocumentGenerator

      public XMLDocumentGenerator(SharedSettings sharedSettings)
      Creates a new XML document generator with default generation settings.
      Parameters:
      sharedSettings - the shared settings containing formatting options (tab size, insert spaces, etc.).
    • XMLDocumentGenerator

      public XMLDocumentGenerator(SharedSettings sharedSettings, XMLGenerationSettings generationSettings)
      Creates a new XML document generator with the given generation settings.
      Parameters:
      sharedSettings - the shared settings containing formatting options.
      generationSettings - the generation settings (maxDepth, optionalElements, etc.).
  • Method Details

    • generate

      public String generate(CMDocument cmDocument, String grammarURI)
      Generates a complete XML document using the first root element declared in the grammar.
      Parameters:
      cmDocument - the content model document loaded from the grammar.
      grammarURI - the grammar file URI (used for grammar binding).
      Returns:
      the generated XML document as a string, or empty string if the grammar has no elements.
    • generate

      public String generate(CMDocument cmDocument, String grammarURI, String rootElementName)
      Generates a complete XML document from the given grammar with the specified root element name.
      Parameters:
      cmDocument - the content model document loaded from the grammar.
      grammarURI - the grammar file URI (used for grammar binding).
      rootElementName - the local name of the root element to generate.
      Returns:
      the generated XML document as a string, or empty string if the root element is not found.