如何读取excel文件替换xml中的字符串并写入新文件java

时间:2023-01-14 07:58:21

A project requires to test web service with the same methods but different host name. I am using Ready API where project is saved as xml file. As there are hundreds of client with different host names that I need to test the related web service. So I am thinking to batch replace host names in the project xml file. Here is the flow: compile a long list of host and other parameters in a excel file, read a row (URL, ID etc.) in the excel file and replace the corresponding string in the xml file and write the xml file with the host name as file name. Then the next row to the same xml file and write the xml file till the end of the excel file in Java or groovy.

项目需要使用相同的方法但不同的主机名测试Web服务。我正在使用Ready API,其中项目保存为xml文件。由于有数百个客户端具有不同的主机名,我需要测试相关的Web服务。所以我想在项目xml文件中批量替换主机名。以下是流程:在excel文件中编译一长串主机和其他参数,读取excel文件中的行(URL,ID等)并替换xml文件中的相应字符串并将xml文件写入主机将文件名命名为。然后将下一行写入相同的xml文件并将xml文件写入Java或groovy中的excel文件的末尾。

3 个解决方案

#1


1  

Another suggestion would be to load and parse the Excel file, modify the specific variables inside of templated API xml request, submit it, and then handle the response. Doing this would allow you change the variables inside of one file without having to regenerate all of the files and would provide you with a more agile workflow.

另一个建议是加载和解析Excel文件,修改模板化API xml请求中的特定变量,提交它,然后处理响应。这样做可以让您更改一个文件中的变量,而无需重新生成所有文件,并为您提供更灵活的工作流程。

I primarily test Web Services at my company and this approach has allowed us to change variables on a moments notice from the research and development teams, and rerun the functional tests extremely quickly.

我主要测试我公司的Web服务,这种方法使我们能够在研发团队的瞬间通知中更改变量,并且可以非常快速地重新运行功能测试。

#2


1  

To...

  1. Read Excel files, use Apache POI.
  2. 读取Excel文件,使用Apache POI。

  3. Parse and modify XML files, use XmlSlurper or XmlParser.
  4. 解析和修改XML文件,使用XmlSlurper或XmlParser。

  5. Write new XML file, use XmlUtil and Java file writing classes.
  6. 编写新的XML文件,使用XmlUtil和Java文件编写类。

#3


1  

I like Emmanuel's answer, but some time ago I came across a great little library called Apache MetaModel. You should be able to use it to both read XLSs and write XMLs, both using a single API. I also concur with Groovy's XmlParser being a great XML tool.

我喜欢Emmanuel的回答,但前段时间我遇到了一个名为Apache MetaModel的小型库。您应该能够使用它来读取XLS和编写XML,两者都使用单个API。我也同意Groovy的XmlParser是一个很棒的XML工具。

#1


1  

Another suggestion would be to load and parse the Excel file, modify the specific variables inside of templated API xml request, submit it, and then handle the response. Doing this would allow you change the variables inside of one file without having to regenerate all of the files and would provide you with a more agile workflow.

另一个建议是加载和解析Excel文件,修改模板化API xml请求中的特定变量,提交它,然后处理响应。这样做可以让您更改一个文件中的变量,而无需重新生成所有文件,并为您提供更灵活的工作流程。

I primarily test Web Services at my company and this approach has allowed us to change variables on a moments notice from the research and development teams, and rerun the functional tests extremely quickly.

我主要测试我公司的Web服务,这种方法使我们能够在研发团队的瞬间通知中更改变量,并且可以非常快速地重新运行功能测试。

#2


1  

To...

  1. Read Excel files, use Apache POI.
  2. 读取Excel文件,使用Apache POI。

  3. Parse and modify XML files, use XmlSlurper or XmlParser.
  4. 解析和修改XML文件,使用XmlSlurper或XmlParser。

  5. Write new XML file, use XmlUtil and Java file writing classes.
  6. 编写新的XML文件,使用XmlUtil和Java文件编写类。

#3


1  

I like Emmanuel's answer, but some time ago I came across a great little library called Apache MetaModel. You should be able to use it to both read XLSs and write XMLs, both using a single API. I also concur with Groovy's XmlParser being a great XML tool.

我喜欢Emmanuel的回答,但前段时间我遇到了一个名为Apache MetaModel的小型库。您应该能够使用它来读取XLS和编写XML,两者都使用单个API。我也同意Groovy的XmlParser是一个很棒的XML工具。