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);
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).
The program xmllint will do the trick:
$ echo '<EmptyTag></EmptyTag>' | xmllint -
<?xml version="1.0"?>
<EmptyTag/>
You could use Java's regular expressions module to replace all occurrences of "<([^/]+?)/>"
with "<\\1></\\1>"
. This will expand the first form ("<EmptyTag/>") to the second form ("<EmptyTag></EmptyTag>").
you can replace "<(\\w+)([^>]*)?>\\s*</\\1>"
with "<$1$2 />"
beforehand
edit or "<(\\w+)( [^/>]*)?/>"
with "<$1$2></$1>"
for the otherway around ;)
There are two options:
- You can use something like XMLUnit to compare the documents to ensure that they semantically equivalent.
- You can read both xml files in using the same parser and then write them back out to a string using the same serializer. The serializer should consistently handle self closing tags.
I would probably use XSLT to tranform both xml-files into the same format, but I don't know if that is the easiest way. There are probably editors that can do formatting for you.
精彩评论