質問

Reading xml from flat file and parsing it using XmlSlurper. Here is how XML file look like

<ClientInformation>
<ContractClientVO>
 <AssignmentReasonCT></AssignmentReasonCT>
 <Associated></Associated>
 <AuthorizedSignatureCT></AuthorizedSignatureCT>
 <ClassCT></ClassCT>
 <RelationshipToInsuredCT></RelationshipToInsuredCT>
 <RelationshipToEmployeeCT></RelationshipToEmployeeCT>
 <ClientRoleVO>
  <AgentFK></AgentFK>
  <Associated></Associated>
  <RoleTypeCT></RoleTypeCT>
  <ClientDetailVO>
    <address></address>
    <city></city>
    <state></state>
    <Amount></Amount>
  </ClientDetailVO>
 </ClientRoleVO>
</ContractClientVO>
</ClientInformation>

XmlSlurper reads it fine and prints the XML document. But it does not navigate XML elements.

def xml = new XmlSlurper().parseText(file)
println XmlUtil.serialize(xml) //outputs fine on console
println xml.ClientInformation.size() //outputs 0
役に立ちましたか?

解決

in your example (once you fix the xml to be valid), the variable xml represents the root node of the document ClientInformation

So calling:

xml.ClientInformation

Returns nothing (as ClientInformation doesn't have a child called ClientInformation)

To get the number of children of the root document tag, you need to just do:

println xml.size()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top