我正在尝试解析一个文件夹中的多个XML文件,并从每个文件中检索String,List的映射。而且我在分离值时遇到问题。
// ### XML文件结构示例
<fields>
<fullName>DandbCompanyId</fullName>
<trackFeedHistory>false</trackFeedHistory>
<type>Lookup</type>
</fields>
<fields>
<fullName>Description</fullName>
<trackFeedHistory>false</trackFeedHistory>
</fields>
<fields>
<fullName>DunsNumber</fullName>
<trackFeedHistory>false</trackFeedHistory>
</fields>
<fields>
<fullName>Fax</fullName>
<trackFeedHistory>false</trackFeedHistory>
</fields>
<fields>
<fullName>Industry</fullName>
<trackFeedHistory>false</trackFeedHistory>
<type>Picklist</type>
</fields>
<fields>
<fullName>IsCustomerPortal</fullName>
</fields>
<fields>
<fullName>IsPartner</fullName>
<trackFeedHistory>false</trackFeedHistory>
</fields>
<listViews>
<fullName>AllAccounts</fullName>
<filterScope>Everything</filterScope>
<label>All Accounts</label>
</listViews>
<listViews>
<fullName>MyAccounts</fullName>
<filterScope>Mine</filterScope>
<label>My Accounts</label>
</listViews>
// ####这是我拥有的
String srcPath = this.args[0];
def objFolder = new File(srcPath + '/objects')
if(!objFolder || !objFolder.isDirectory()) {
println " #### ERROR: ${objFolder} doesn't exist or is not a folder"
return
} else {
println " #### Path is correct! Checking objects"
}
def objMap = [:]
def xs = new XmlSlurper()
objFolder.eachFile { file ->
objMap.put(file.getName().split("\\.")[0], xs.parse(file).fields.fullName.each{
it.@fullName
})
};
objMap.each { k,v -> println v}
// ###这是输出
DescriptionIsAlohaSupportedIsLightningSupportedNameOwnerIdStartingContext
IndustryOwnership
AllocationIdAmountBudgetIdChannelPartnerIdDescriptionOwnerIdRequestIdStatusTitle
ScorecardIdTargetEntityId
ActivityAllocationIdAmountBudgetIdCampaignIdChannelPartnerIdDescriptionDesiredOutcomeOwnerIdRequestedAmountStatusTitleTotalApprovedFcsTotalReimbursedFcs
CategoriesStatus
CapturedAngleContentDocumentIdImageAlternateTextImageClassImageClassObjectTypeImageTitleImageUrlImageViewTypeIsActiveName
Role
我旨在实现的目标是:
locationId,Amount,BudgetId,渠道
因此它应该看起来像一个字符串列表(在映射中将是值)
答案 0 :(得分:1)
您尝试的内容有两个问题。
第一个方法是使用each
对集合中的每个项目执行操作,但是它返回原始集合(不是操作的结果-为此,您需要collect
)< / p>
第二个是it.@fullName
将在标签上返回属性fullName
(即:如果您有<node fullName="something">...</node>
),而您只需要节点的text()
< / p>
所以以下方法会起作用:
xs.parse(file).fields.fullName.collect { it.text() }
但是在Groovy中,我们可以缩短collect
来使用扩展操作符*
,这使我们更短:
xs.parse(file).fields.fullName*.text()
玩得开心!