0

I have a case in my Java EE application like I need to compare two large auto-generated XMLs. I just need to check if both the XMLs are equal (tags and values).

I tried using XMLUnit, but the thing is like it returns false even if there are spaces between tags(these XMLs are auto generated right!). Is there any effective way to do this or to write down our own logic ?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Bash
  • 21
  • 3
  • 8

4 Answers4

1

I would suggest StAX. It is the best to work with large files. This could be someting like this

private boolean compare(XMLEventReader xr1, XMLEventReader xr2) throws XMLStreamException {
    for (;;) {
        XMLEvent e1 = nextTag(xr1);
        XMLEvent e2 = nextTag(xr2);
        if (e1 == null || e2 == null) {
            return e1 == e2;
        }
        if (!equals(e1, e2)) {
            return false;
        }
    }
}

private static XMLEvent nextTag(XMLEventReader xr) throws XMLStreamException {
    while (xr.hasNext()) {
        XMLEvent e = xr.nextEvent();
        if (e.getEventType() == XMLStreamConstants.START_ELEMENT) {
            return e;
        }
    }
    return null;
}

private boolean equals(XMLEvent e1, XMLEvent e2) {
    // compare attributes and content
    return false;
}
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

convert the xml to POJO by parsing, using jaxb

Implement the object.equals() in above POJO

then compare the parsed objects using Comparator

TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
0

You could use an editor with a built-in diff tool. I use the netbeans editor to diff compare files. I find it quick and easy.

Dark Star1
  • 6,986
  • 16
  • 73
  • 121
0

You can try with:

XMLUnit.setIgnoreWhitespace(Boolean.TRUE);

XMLUnit.setNormalizeWhitespace(Boolean.TRUE);

That should work for now.

Theo Leinad
  • 115
  • 1
  • 7