The `XmlSnapshotService` can be used to convert a domain object into XML . It can also be used to convert a graph of domain objects into XML, rooted at a starting domain object.

One use case of the resultant XML, is for auditing, where there is a requirement to capture the state of a domain object or domain objects into a single artifact. This XML could for example be hashed and digitally signed, to form the basis of a legal decision.

| It is possible to explicitly exclude properties from the snapshot using the `@Property(snapshot=…​)` element.

In addition to generating XML, the service can also generate an XSD schema that the XML will be conformant with. The original use case here was in support of mapping tools used to generate PDF documents (mail merges).

| The XML is returned by `XmlSnapshotService` as an instance of `org.w3c.dom.Document`. The related `XmlService` domain service can be used to convert this into a string.

## Example View model

The `XmlSnapshotParentVm` is used as the root of snapshots. It references a collection of objects as its children, and another single domain object as its peer.

Figure 1. `XmlSnapshotVm` is the root of the snapshots

## XML Snapshot

The `takeXmlSnapshot` service demonstrates how to create snapshots of different "depths":

    @Action(
        semantics = SemanticsOf.SAFE
    )
    @RequiredArgsConstructor
    public class XmlSnapshotParentVm_takeXmlSnapshot {

        @Inject XmlSnapshotService xmlSnapshotService;
        @Inject XmlService xmlService;
        // ...

        public Clob act(
                final PathsToInclude pathsToInclude,
                final SnapshotType snapshotType) {
            val builder = xmlSnapshotService.builderFor(xmlSnapshotParentVm);
            for (String path : pathsToInclude.paths) {
                builder.includePath(path);
            }
            val snapshot = builder.build();
            val doc = snapshotType == SnapshotType.XML
                    ? snapshot.getXmlDocument() : snapshot.getXsdDocument();
            val fileName = String.format("%s.%s", pathsToInclude.name(), snapshotType.name().toLowerCase());
            return asClob(fileName, xmlService.asString(doc));
        }
        // ...
    }

This uses two enums:

- the `PathsToInclude` enum is used to indicate which members to include in the snapshot:

      public enum PathsToInclude {
          NONE,
          PEER("peer"),
          CHILDREN("children"),
          PEER_AND_CHILDREN("peer", "children"),
          PEER_AND_ITS_CHILDREN("peer/children");

          final List<String> paths;
          PathsToInclude(String... paths) {
              this.paths = Collections.unmodifiableList(Arrays.asList(paths));
          }
      }

  Note that this is in fact a path. In particular, the `peer/children` path will include both the peer object and the peer object’s children in the resultant snapshot

- The `SnapshotType` enum is simply:

      public enum SnapshotType {
          XML,
          XSD
      }

  This is used in the action to determine whether to generate the XML snapshot (of the values of the domain object) or an XSD document (with which the XML document is conformant).