简体   繁体   English

将XML自关闭标记替换为空标记

[英]Replace XML self-closing tag with empty one

I have to compare XML data. 我必须比较XML数据。 There are two sources- 有两个来源 -

  • Web Service 网络服务
  • XML files XML文件

I don't see any easy way to transform them in same classes and use equals method. 我没有看到任何简单的方法来在同一个类中转换它们并使用equals方法。

The classes that work with Web Services are auto generated and WSDL isn't simple at all. 使用Web服务的类是自动生成的,WSDL根本不简单。

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. 所以我从Web Service读取响应,读取相应的文件,将它们转换为具有相同格式的String(删除空格,\\ n \\ r \\ n字符等),然后使用String.equals()方法。

The issue is the Web services's empty tags are written next way : 问题是Web服务的空标签是下一个写的:

<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 UPDATE

I don't parse the xml. 我不解析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: Web Service响应中的对象结构将以下一种方式转换为xml字符串:

    marshaller.marshal(new JAXBElement<response_class_name>(new QName("response_class_name"),
       response_class_name.class, response_object), stringWriter);

对于Java,我会使用XMLUnit来比较文件,因为它使用它们的结构比较xml文件,而不是字符串(它可能会也可能不会忽略空格,具体取决于设置)。

You could use Java's regular expressions module to replace all occurrences of "<([^/]+?)/>" with "<\\\\1></\\\\1>" . 您可以使用Java的正则表达式模块将所有出现的"<([^/]+?)/>"替换为"<\\\\1></\\\\1>" This will expand the first form ("<EmptyTag/>") to the second form ("<EmptyTag></EmptyTag>"). 这会将第一种形式(“<EmptyTag />”)扩展为第二种形式(“<EmptyTag> </ EmptyTag>”)。

you can replace "<(\\\\w+)([^>]*)?>\\\\s*</\\\\1>" with "<$1$2 />" beforehand 你可以预先用"<$1$2 />"替换"<(\\\\w+)([^>]*)?>\\\\s*</\\\\1>"

edit or "<(\\\\w+)( [^/>]*)?/>" with "<$1$2></$1>" for the otherway around ;) 编辑"<(\\\\w+)( [^/>]*)?/>""<$1$2></$1>"为其他方面;)

The program xmllint will do the trick: 程序xmllint可以解决这个问题:

$ echo '<EmptyTag></EmptyTag>' | xmllint -
<?xml version="1.0"?>
<EmptyTag/>

There are two options: 有两种选择:

  1. You can use something like XMLUnit to compare the documents to ensure that they semantically equivalent. 您可以使用类似XMLUnit的内容来比较文档,以确保它们在语义上等效。
  2. You can read both xml files in using the same parser and then write them back out to a string using the same serializer. 您可以使用相同的解析器读取两个xml文件,然后使用相同的序列化程序将它们写回字符串。 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. 我可能会使用XSLT将两个xml文件转换为相同的格式,但我不知道这是否是最简单的方法。 There are probably editors that can do formatting for you. 可能有编辑器可以为您进行格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM