Class XmlDiff


  • public final class XmlDiff
    extends java.lang.Object
    XML-aware structural diff engine that compares two documents and detects both semantic and formatting-only changes.

    Because DomTrip preserves all formatting metadata (whitespace, quote styles, entity encoding, empty element style), this diff can uniquely distinguish between changes that affect meaning and changes that are formatting-only. This is a capability no other Java XML library offers.

    Basic usage:

    
     Document before = Document.of(oldXml);
     Document after = Document.of(newXml);
    
     DiffResult diff = XmlDiff.diff(before, after);
    
     for (XmlChange change : diff.changes()) {
         System.out.println(change);
     }
     // → ELEMENT_ADDED: /project/dependencies/dependency[3]
     // → TEXT_CHANGED: /project/version: "1.0" → "1.1"
     // → ATTRIBUTE_CHANGED: /project/dependencies/dependency[2]/@scope: "compile" → "test"
     

    Configurable matching:

    
     DiffConfig config = DiffConfig.builder()
         .matchBy("dependency", "groupId", "artifactId")
         .build();
    
     DiffResult diff = XmlDiff.diff(before, after, config);
     
    Since:
    1.3.0
    See Also:
    DiffResult, DiffConfig, XmlChange, ChangeType
    • Method Detail

      • diff

        public static DiffResult diff​(Document before,
                                      Document after)
        Compares two documents using default configuration (positional matching).
        Parameters:
        before - the original document
        after - the modified document
        Returns:
        the diff result containing all detected changes
      • diff

        public static DiffResult diff​(Document before,
                                      Document after,
                                      DiffConfig config)
        Compares two documents using the given configuration.
        Parameters:
        before - the original document
        after - the modified document
        config - the diff configuration controlling element matching
        Returns:
        the diff result containing all detected changes