gpt4 book ai didi

mysql - SOAPUI Groovy 解析 XML 响应并将其插入 mysql 数据库

转载 作者:行者123 更新时间:2023-11-29 11:41:12 26 4
gpt4 key购买 nike

我是 groovy 的初学者,正在尝试解析 XML 并将结果记录插入 MySQL 数据库示例 SOAP XML 响应

 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetCitiesByCountryResponse xmlns="http://www.webserviceX.NET">
<GetCitiesByCountryResult><NewDataSet>
<Table>
<Country>British Indian Ocean Territory</Country>
<City>Diego Garcia</City>
</Table>
<Table>
<Country>India</Country>
<City>Ahmadabad</City>
</Table>
<Table>
<Country>India</Country>
<City>Akola</City>
</Table>
</NewDataSet></GetCitiesByCountryResult>
</GetCitiesByCountryResponse>
</soap:Body>
</soap:Envelope>

SOAPUI GROOVY 测试步骤脚本

import groovy.sql.Sql
def sql = Sql.newInstance('jdbc:mysql://localhost:3306/weather', 'root', 'security', 'com.mysql.jdbc.Driver')
def response = context.expand( '${GetCitiesByCountry#Response}' )
def xml = new XmlSlurper().parseText(response).Body.GetCitiesByCountryResponse.GetCitiesByCountryResult.NewDataSet.Table
//log.info xml
xml.each{ node ->
sql.execute("INSERT INTO indcit(country,city) VALUES (?,?)" ,[node.Country, node.City])
//sql.execute("INSERT INTO indcit(country,city) VALUES (${node.Country},${node.City})")
//log.info node.Country
//log.info node.City
}

尝试时,两个 sql.execute 行都会抛出以下相同的错误 java.sql.SQLException:Invalid argument value: java.io.NOTSerializedException

错误日志

Thu Mar 03 01:16:36 IST 2016:ERROR:java.sql.SQLException: Invalid argument value: java.io.NotSerializableException
java.sql.SQLException: Invalid argument value: java.io.NotSerializableException
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.PreparedStatement.setSerializableObject(PreparedStatement.java:3415)
at com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:3066)
at groovy.sql.Sql.setObject(Sql.java:3655)
at groovy.sql.Sql.setParameters(Sql.java:3620)
at groovy.sql.Sql.getPreparedStatement(Sql.java:3881)
at groovy.sql.Sql.getPreparedStatement(Sql.java:3928)
at groovy.sql.Sql.execute(Sql.java:2287)
at groovy.sql.Sql$execute.call(Unknown Source)
at Script62$_run_closure1.doCall(Script62.groovy:7)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:909)
at groovy.lang.Closure.call(Closure.java:411)
at groovy.lang.Closure.call(Closure.java:427)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1325)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1297)
at org.codehaus.groovy.runtime.dgm$148.doMethodInvoke(Unknown Source)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1085)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:909)
at groovy.lang.DelegatingMetaClass.invokeMethod(DelegatingMetaClass.java:149)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:39)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:54)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at Script62.run(Script62.groovy:6)
at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:92)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:141)
at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:250)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

感谢您提前提供的任何帮助!

最佳答案

尝试在您的节点上调用text(),即:

sql.execute("INSERT INTO indcit(country,city) VALUES (?,?)" ,[node.Country.text(), node.City.text()])

关于mysql - SOAPUI Groovy 解析 XML 响应并将其插入 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35756850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com