Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace XML self-closing tag with empty one

Tags:

java

compare

xml

I have to compare XML data. There are two sources-

  • Web Service
  • XML files

I don't see any easy way to transform them in same classes and use equals method.

The classes that work with Web Services are auto generated and WSDL isn't simple at all.

So I read the response from Web Service, read the corresponding file, transform them to String with the same formatting ( removed spaces, \n\r characters, and so on ) and then use String.equals() method.

The issue is the Web services's empty tags are written next way :

<EmptyTag/>

but provided files contains this kind of empty tags:

<EmptyTag></EmptyTag>

OK, there is a way to prepare all provided files manually, but I don't like it. Who knows, how it's possible to transform empty tags to the same style ? If there are any ideas how to simplify to process - you are welcome ;)

UPDATE

I don't parse the xml. The file's data is just read and transformed to expected format. The object's structure from Web Service's response is transformed to xml string in the next way:

    marshaller.marshal(new JAXBElement<response_class_name>(new QName("response_class_name"),
       response_class_name.class, response_object), stringWriter);
like image 208
StKiller Avatar asked May 01 '26 19:05

StKiller


1 Answers

For Java I would use XMLUnit to compare the files, as it compares xml files using their structure, not as strings (it may or may not ignore whitespace, depending on settings).

like image 133
Kathy Van Stone Avatar answered May 03 '26 07:05

Kathy Van Stone