Xml 解析未生成预期的输出
我正在使用Groovy解析以下文件。我使用的代码部分是在输出的末尾连接person角色,即使person角色正在循环中处理Xml 解析未生成预期的输出,xml,parsing,groovy,Xml,Parsing,Groovy,我正在使用Groovy解析以下文件。我使用的代码部分是在输出的末尾连接person角色,即使person角色正在循环中处理 def Sample=""" <tptc:People xmlns:tptc="http://www.metadata.calcum-inc.com/stats/"> <tptc:Identifier>456-99088</tptc:Identifier> <tptc:Name>The
def Sample="""
<tptc:People xmlns:tptc="http://www.metadata.calcum-inc.com/stats/">
<tptc:Identifier>456-99088</tptc:Identifier>
<tptc:Name>The Grand Supper</tptc:Name>
<tptc:Person>
<tptc:PersonRole>
<tptc:PersonRoleCode>COMMANDER</tptc:PersonRoleCode>
<tptc:PersonRoleDescription>
IN CHARGE OF ARMY
</tptc:PersonRoleDescription>
</tptc:PersonRole>
<tptc:PersonRole>
<tptc:PersonRoleCode>CONDUCTOR</tptc:PersonRoleCode>
<tptc:PersonRoleDescription>
DIRECTS SYMPHONY
</tptc:PersonRoleDescription>
</tptc:PersonRole>
<tptc:PersonName>
<tptc:PersonFirstName>Jackie</tptc:PersonFirstName>
<tptc:PersonMiddleName>Wang</tptc:PersonMiddleName>
<tptc:PersonLastName>Mang</tptc:PersonLastName>
</tptc:PersonName>
<tptc:PersonAddress>
<tptc:PersonAddressLine1Text>String</tptc:PersonAddressLine1Text>
<tptc:PersonAddressLine2Text>String</tptc:PersonAddressLine2Text>
<tptc:PersonCityName>String</tptc:PersonCityName>
<tptc:PersonStateCode>String</tptc:PersonStateCode>
<tptc:PersonZip5Code>String</tptc:PersonZip5Code>
<tptc:PersonZip4Code>String</tptc:PersonZip4Code>
</tptc:PersonAddress>
<tptc:PersonTelephone>
<tptc:PersonAreaCode>String</tptc:PersonAreaCode>
<tptc:PersonTelephoneNumber>String</tptc:PersonTelephoneNumber>
<tptc:TelephoneTypeCode>String</tptc:TelephoneTypeCode>
</tptc:PersonTelephone>
<tptc:PersonTelephone>
<tptc:PersonAreaCode>String</tptc:PersonAreaCode>
<tptc:PersonTelephoneNumber>String</tptc:PersonTelephoneNumber>
<tptc:TelephoneTypeCode>String</tptc:TelephoneTypeCode>
</tptc:PersonTelephone>
</tptc:Person>
<tptc:Person>
<tptc:PersonRole>
<tptc:PersonRoleCode>First</tptc:PersonRoleCode>
<tptc:PersonRoleDescription>Best Best</tptc:PersonRoleDescription>
</tptc:PersonRole>
<tptc:PersonRole>
<tptc:PersonRoleCode>None</tptc:PersonRoleCode>
<tptc:PersonRoleDescription>Nonetity</tptc:PersonRoleDescription>
</tptc:PersonRole>
<tptc:PersonName>
<tptc:PersonFirstName>String</tptc:PersonFirstName>
<tptc:PersonMiddleName>String</tptc:PersonMiddleName>
<tptc:PersonLastName>String</tptc:PersonLastName>
</tptc:PersonName>
<tptc:PersonAddress>
<tptc:PersonAddressLine1Text>String</tptc:PersonAddressLine1Text>
<tptc:PersonAddressLine2Text>String</tptc:PersonAddressLine2Text>
<tptc:PersonCityName>String</tptc:PersonCityName>
<tptc:PersonStateCode>String</tptc:PersonStateCode>
<tptc:PersonZip5Code>String</tptc:PersonZip5Code>
<tptc:PersonZip4Code>String</tptc:PersonZip4Code>
</tptc:PersonAddress>
<tptc:PersonTelephone>
<tptc:PersonAreaCode>String</tptc:PersonAreaCode>
<tptc:PersonTelephoneNumber>
String
</tptc:PersonTelephoneNumber>
<tptc:TelephoneTypeCode>
String
</tptc:TelephoneTypeCode>
</tptc:PersonTelephone>
<tptc:PersonTelephone>
<tptc:PersonAreaCode>String</tptc:PersonAreaCode>
<tptc:PersonTelephoneNumber>
String
</tptc:PersonTelephoneNumber>
<tptc:TelephoneTypeCode>
String
</tptc:TelephoneTypeCode>
</tptc:PersonTelephone>
</tptc:Person>
</tptc:People>
"""
以下是输出:
COMMANDER
IN CHARGE OF ARMY
CONDUCTOR
DIRECTS SYMPHONY
COMMANDERIN CHARGE OF ARMYCONDUCTORDIRECTS SYMPHONY
First
Best Best
None
Nonetity
FirstBest BestNoneNonetity
Result: COMMANDERIN CHARGE OF ARMYCONDUCTORDIRECTS SYMPHONYJackieWangMangStringStringStringStringStringStringStringStringStringStringStringStringFirstBest BestNoneNonetityStringStringStringStringStringStringStringString
StringStringStringStringStringMANICString
它将连接Person元素输出的所有其余部分。我只希望它打印我关心的元素,即循环中的代码。有没有办法做到这一点?救命啊 这是因为,在您的代码之后没有任何内容 你可以在下面的脚本。这个脚本实际做的是收集
角色和描述
,然后最终显示结果
defxml=”“”
456-99088
盛大的晚餐
指挥官
掌管军队
导体
指挥交响乐
杰基
王
莽
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
弗斯特
最好的
没有一个
非实体
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
"""
def parsedXml=new XmlSlurper().parseText(xml)
def ppl=parsedXml.'**'.findAll{it.name()=='Person'}
def结果=[]
ppl.each{peop->
peop.'**'.findAll{it.name()='PersonRole'}。每个{role->
def detail=[]
详细信息这是因为,代码后面没有任何内容
你可以在下面的脚本中找到答案。这个脚本实际做的是收集角色和描述
,然后最终显示结果
defxml=”“”
456-99088
盛大的晚餐
指挥官
掌管军队
导体
指挥交响乐
杰基
王
莽
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
弗斯特
最好的
没有一个
非实体
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
一串
"""
def parsedXml=new XmlSlurper().parseText(xml)
def ppl=parsedXml.'**'.findAll{it.name()=='Person'}
def结果=[]
ppl.each{peop->
peop.'**'.findAll{it.name()='PersonRole'}。每个{role->
def detail=[]
详细说明您的预期产出是什么?您的预期产出是什么?非常感谢。非常感谢这个解决方案。很高兴知道它有帮助。如果有帮助,请投票支持。非常感谢。非常感谢这个解决方案。很高兴知道它有帮助。如果有帮助,请投票支持。
COMMANDER
IN CHARGE OF ARMY
CONDUCTOR
DIRECTS SYMPHONY
COMMANDERIN CHARGE OF ARMYCONDUCTORDIRECTS SYMPHONY
First
Best Best
None
Nonetity
FirstBest BestNoneNonetity
Result: COMMANDERIN CHARGE OF ARMYCONDUCTORDIRECTS SYMPHONYJackieWangMangStringStringStringStringStringStringStringStringStringStringStringStringFirstBest BestNoneNonetityStringStringStringStringStringStringStringString
StringStringStringStringStringMANICString
[[COMMANDER, IN CHARGE OF ARMY], [CONDUCTOR, DIRECTS SYMPHONY], [First, Best Best], [None, Nonetity]]