Forum Discussion

karelkremel's avatar
karelkremel
Occasional Contributor
11 years ago

how to work with XML in JDBC response

I'm using JDBC request for getting XMLworkflows from database.

the results looks like
<Results>
<ResultSet fetchSize="10">
<Row rowNumber="1">
<NAME>CLASSIC_AUDIT</NAME>
<XML_DEFINITION>
&lt;statechart name="CLASSIC_AUDIT" isSubStateChart="true"&gt;
&lt;actions&gt;
&lt;action name="AUDIT_INSTANCE" classname="gp.AuditInstance" /&gt;
&lt;action name="ANALYZE_RESPONSES" classname="analyzer.AnalyzeResponses" /&gt;
&lt;action name="GET_STATE_AND_PARENT" classname="actions.GetAOStateAndParent" /&gt;
&lt;/actions&gt;
&lt;/statechart&gt;
</XML_DEFINITION>
</Row>
<Row>........................................................

how to access the XML inside the //Result[1]/ResultSet[1]/Row[1]/XML_DEFINITION[1]?

For example i need to create assertion to verify the class for AUDIT_INSTANCE action

pls help

3 Replies

  • PaulDonny's avatar
    PaulDonny
    Regular Contributor
    Well if it is in a string format I would just use regex. The following will return gp.AuditInstance. I also set it up to be able to recognize just that one line in case you wanted to do further formatting or testing with it.

    RegexPlanet is a great resource just ensure that the matches() field is yes if your using the regex assertion step.

    (?s).*\n.*?AUDIT_INSTANCE.*?classname="(.*?)".*?\n.*
  • You could also parse the xml for the inner xml, see example below.


    ​s="""
    <Results>
    <ResultSet fetchSize="10">
    <Row rowNumber="1">
    <NAME>CLASSIC_AUDIT</NAME>
    <XML_DEFINITION>
    &lt;statechart name="CLASSIC_AUDIT" isSubStateChart="true"&gt;
    &lt;actions&gt;
    &lt;action name="AUDIT_INSTANCE" classname="gp.AuditInstance" /&gt;
    &lt;action name="ANALYZE_RESPONSES" classname="analyzer.AnalyzeResponses" /&gt;
    &lt;action name="GET_STATE_AND_PARENT" classname="actions.GetAOStateAndParent" /&gt;
    &lt;/actions&gt;
    &lt;/statechart&gt;
    </XML_DEFINITION>
    </Row></ResultSet></Results>"""

    def xml = new XmlParser().parseText(s)
    System.out.println("innerxml as string: " + xml.ResultSet[0].Row[0].XML_DEFINITION[0].text())

    //just load the inner xml
    def innerxml = new XmlParser().parseText(new XmlParser().parseText(s).ResultSet[0].Row[0].XML_DEFINITION[0].text())
    System.out.println("tagname: " + innerxml.actions[0].name())
    System.out.println("class: " + innerxml.actions[0].action[0]."@name")

    assert "gp.AuditInstance" == innerxml.actions[0].action.findAll{ it."@name" == "AUDIT_INSTANCE"}[0]."@classname"

    System.out.println("done")