2017-09-06 54 views
0

背景:嘗試從XML獲取所有屬性和值。我試過使用XmlParser以及XmlSlurper。我接受任何其他建議(這是JIRA的XML工作流程,試圖將值放入報告中)。解析來自嵌套XML的屬性和值

運行屬性()在它上面不會返回頂層的任何東西。因此,將它運行到每個()將使其可用。前三個返回值正常工作,但下一個「級別」不會,除非在每個後續數組上運行。我不確定該怎麼做。每個工作流程都會有所不同。我想過某種正則表達式,但似乎並不適用於不同的工作流程長度和類型。這是一個非常基本的工作流程。其他更復雜,步驟和屬性的數量也會有所不同。
運行children方法將返回值,但它是一個列表,而不是在節點類中。我把它放在這裏來表明它會爲價值觀返回什麼。有什麼建議麼?

def xml = 
    //'<?xml version="1.0" encoding="UTF-8"?>\n' + 
    //'<!DOCTYPE workflow PUBLIC "-//OpenSymphony Group//DTD OSWorkflow 2.8//EN" "http://www.opensymphony.com/osworkflow/workflow_2_8.dtd">\n' + 
    '<workflow>\n' + 
      ' <meta name="jira.description"></meta>\n' + 
      ' <meta name="jira.update.author.key">admin</meta>\n' + 
      ' <meta name="jira.updated.date">1503954892468</meta>\n' + 
      ' <initial-actions>\n' + 
      ' <action id="1" name="Create">\n' + 
      '  <meta name="jira.i18n.submit">common.forms.create</meta>\n' + 
      '  <meta name="jira.i18n.title">common.forms.create</meta>\n' + 
      '  <validators>\n' + 
      '  <validator name="" type="class">\n' + 
      '   <arg name="permission">Create Issue</arg>\n' + 
      '   <arg name="class.name">com.atlassian.jira.workflow.validator.PermissionValidator</arg>\n' + 
      '  </validator>\n' + 
      '  </validators>\n' + 
      '  <results>\n' + 
      '  <unconditional-result old-status="null" status="open" step="1">\n' + 
      '   <post-functions>\n' + 
      '   <function type="class">\n' + 
      '    <arg name="class.name">com.atlassian.jira.workflow.function.issue.IssueCreateFunction</arg>\n' + 
      '   </function>\n' + 
      '   <function type="class">\n' + 
      '    <arg name="class.name">com.atlassian.jira.workflow.function.issue.IssueReindexFunction</arg>\n' + 
      '   </function>\n' + 
      '   <function type="class">\n' + 
      '    <arg name="eventTypeId">1</arg>\n' + 
      '    <arg name="class.name">com.atlassian.jira.workflow.function.event.FireIssueEventFunction</arg>\n' + 
      '   </function>\n' + 
      '   </post-functions>\n' + 
      '  </unconditional-result>\n' + 
      '  </results>\n' + 
      ' </action>\n' + 
      ' </initial-actions>\n' + 
      ' <steps>\n' + 
      ' <step id="1" name="To Do">\n' + 
      '  <meta name="jira.status.id">10000</meta>\n' + 
      '  <actions>\n' + 
      '  <action id="11" name="Start Progress">\n' + 
      '   <meta name="jira.i18n.submit">startprogress.title</meta>\n' + 
      '   <meta name="jira.description"></meta>\n' + 
      '   <meta name="jira.i18n.title">startprogress.title</meta>\n' + 
      '   <results>\n' + 
      '   <unconditional-result old-status="Not Done" status="Done" step="2">\n' + 
      '    <post-functions>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowupdateissuestatus-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.UpdateIssueStatusFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowassigntocurrentuser-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.AssignToCurrentUserFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowcreatecomment-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.misc.CreateCommentFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowgeneratechangehistory-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.GenerateChangeHistoryFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowreindexissue-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.IssueReindexFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="eventTypeId">13</arg>\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowfireevent-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.event.FireIssueEventFunction</arg>\n' + 
      '    </function>\n' + 
      '    </post-functions>\n' + 
      '   </unconditional-result>\n' + 
      '   </results>\n' + 
      '  </action>\n' + 
      '  <action id="21" name="Done">\n' + 
      '   <meta name="jira.i18n.submit">jira.issuetracking.simple.workflow.action.done.name</meta>\n' + 
      '   <meta name="jira.description"></meta>\n' + 
      '   <meta name="jira.i18n.title">jira.issuetracking.simple.workflow.action.done.name</meta>\n' + 
      '   <restrict-to>\n' + 
      '   <conditions>\n' + 
      '    <condition type="class">\n' + 
      '    <arg name="permission">RESOLVE_ISSUES</arg>\n' + 
      '    <arg name="class.name">com.atlassian.jira.workflow.condition.PermissionCondition</arg>\n' + 
      '    </condition>\n' + 
      '   </conditions>\n' + 
      '   </restrict-to>\n' + 
      '   <results>\n' + 
      '   <unconditional-result old-status="Not Done" status="Done" step="3">\n' + 
      '    <post-functions>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="field.name">resolution</arg>\n' + 
      '     <arg name="field.value">10000</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.UpdateIssueFieldFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowupdateissuestatus-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.UpdateIssueStatusFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowcreatecomment-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.misc.CreateCommentFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowgeneratechangehistory-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.GenerateChangeHistoryFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowreindexissue-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.IssueReindexFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="eventTypeId">13</arg>\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowfireevent-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.event.FireIssueEventFunction</arg>\n' + 
      '    </function>\n' + 
      '    </post-functions>\n' + 
      '   </unconditional-result>\n' + 
      '   </results>\n' + 
      '  </action>\n' + 
      '  </actions>\n' + 
      ' </step>\n' + 
      ' <step id="2" name="In Progress">\n' + 
      '  <meta name="jira.status.id">3</meta>\n' + 
      '  <actions>\n' + 
      '  <action id="31" name="Stop Progress">\n' + 
      '   <meta name="jira.i18n.submit">stopprogress.title</meta>\n' + 
      '   <meta name="jira.description"></meta>\n' + 
      '   <meta name="jira.i18n.title">stopprogress.title</meta>\n' + 
      '   <results>\n' + 
      '   <unconditional-result old-status="Not Done" status="Done" step="1">\n' + 
      '    <post-functions>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowupdateissuestatus-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.UpdateIssueStatusFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowcreatecomment-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.misc.CreateCommentFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowgeneratechangehistory-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.GenerateChangeHistoryFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowreindexissue-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.IssueReindexFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="eventTypeId">13</arg>\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowfireevent-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.event.FireIssueEventFunction</arg>\n' + 
      '    </function>\n' + 
      '    </post-functions>\n' + 
      '   </unconditional-result>\n' + 
      '   </results>\n' + 
      '  </action>\n' + 
      '  <action id="41" name="Done">\n' + 
      '   <meta name="jira.i18n.submit">jira.issuetracking.simple.workflow.action.done.name</meta>\n' + 
      '   <meta name="jira.description"></meta>\n' + 
      '   <meta name="jira.i18n.title">jira.issuetracking.simple.workflow.action.done.name</meta>\n' + 
      '   <restrict-to>\n' + 
      '   <conditions>\n' + 
      '    <condition type="class">\n' + 
      '    <arg name="permission">RESOLVE_ISSUES</arg>\n' + 
      '    <arg name="class.name">com.atlassian.jira.workflow.condition.PermissionCondition</arg>\n' + 
      '    </condition>\n' + 
      '   </conditions>\n' + 
      '   </restrict-to>\n' + 
      '   <results>\n' + 
      '   <unconditional-result old-status="Not Done" status="Done" step="3">\n' + 
      '    <post-functions>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="field.name">resolution</arg>\n' + 
      '     <arg name="field.value">10000</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.UpdateIssueFieldFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowupdateissuestatus-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.UpdateIssueStatusFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowcreatecomment-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.misc.CreateCommentFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowgeneratechangehistory-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.GenerateChangeHistoryFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowreindexissue-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.IssueReindexFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="eventTypeId">13</arg>\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowfireevent-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.event.FireIssueEventFunction</arg>\n' + 
      '    </function>\n' + 
      '    </post-functions>\n' + 
      '   </unconditional-result>\n' + 
      '   </results>\n' + 
      '  </action>\n' + 
      '  </actions>\n' + 
      ' </step>\n' + 
      ' <step id="3" name="Done">\n' + 
      '  <meta name="jira.status.id">10001</meta>\n' + 
      '  <actions>\n' + 
      '  <action id="51" name="Reopen">\n' + 
      '   <meta name="jira.i18n.submit">jira.issuetracking.simple.workflow.action.reopen.name</meta>\n' + 
      '   <meta name="jira.description"></meta>\n' + 
      '   <meta name="jira.i18n.title">jira.issuetracking.simple.workflow.action.reopen.name</meta>\n' + 
      '   <restrict-to>\n' + 
      '   <conditions>\n' + 
      '    <condition type="class">\n' + 
      '    <arg name="permission">RESOLVE_ISSUES</arg>\n' + 
      '    <arg name="class.name">com.atlassian.jira.workflow.condition.PermissionCondition</arg>\n' + 
      '    </condition>\n' + 
      '   </conditions>\n' + 
      '   </restrict-to>\n' + 
      '   <results>\n' + 
      '   <unconditional-result old-status="Not Done" status="Done" step="1">\n' + 
      '    <post-functions>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="field.name">resolution</arg>\n' + 
      '     <arg name="field.value"></arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.UpdateIssueFieldFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowupdateissuestatus-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.UpdateIssueStatusFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowcreatecomment-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.misc.CreateCommentFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowgeneratechangehistory-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.GenerateChangeHistoryFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowreindexissue-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.IssueReindexFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="eventTypeId">13</arg>\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowfireevent-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.event.FireIssueEventFunction</arg>\n' + 
      '    </function>\n' + 
      '    </post-functions>\n' + 
      '   </unconditional-result>\n' + 
      '   </results>\n' + 
      '  </action>\n' + 
      '  <action id="61" name="Reopen and start progress">\n' + 
      '   <meta name="jira.i18n.submit">jira.issuetracking.simple.workflow.action.reopenandstartprogress.name</meta>\n' + 
      '   <meta name="jira.description"></meta>\n' + 
      '   <meta name="jira.i18n.title">jira.issuetracking.simple.workflow.action.reopenandstartprogress.name</meta>\n' + 
      '   <results>\n' + 
      '   <unconditional-result old-status="Not Done" status="Done" step="2">\n' + 
      '    <post-functions>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="field.name">resolution</arg>\n' + 
      '     <arg name="field.value"></arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.UpdateIssueFieldFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowupdateissuestatus-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.UpdateIssueStatusFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowassigntocurrentuser-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.AssignToCurrentUserFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowcreatecomment-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.misc.CreateCommentFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowgeneratechangehistory-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.GenerateChangeHistoryFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowreindexissue-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.issue.IssueReindexFunction</arg>\n' + 
      '    </function>\n' + 
      '    <function type="class">\n' + 
      '     <arg name="eventTypeId">13</arg>\n' + 
      '     <arg name="full.module.key">com.atlassian.jira.plugin.system.workflowfireevent-function</arg>\n' + 
      '     <arg name="class.name">com.atlassian.jira.workflow.function.event.FireIssueEventFunction</arg>\n' + 
      '    </function>\n' + 
      '    </post-functions>\n' + 
      '   </unconditional-result>\n' + 
      '   </results>\n' + 
      '  </action>\n' + 
      '  </actions>\n' + 
      ' </step>\n' + 
      ' </steps>\n' + 
      '</workflow>\n' + 
      '' 

def doc = new XmlParser().parseText(xml) 
def attr = doc.attributes() //no results 

doc.each { 

    if (it.attributes()) { 
     println(it.name()) 
     println(it.attributes()) 
     println(it.value()) 
     println "" 
    } else { 
     println (it.children()) 

    } 

    } 

輸出:

meta 
[name:jira.description] 
[] 

meta 
[name:jira.update.author.key] 
[admin] 

meta 
[name:jira.updated.date] 
[1503954892468] 

[action[attributes={id=1, name=Create}; value=[meta[attributes={name=jira.i18n.submit}; value=[common.forms.create]], meta[attributes={name=jira.i18n.title}; value=[common.forms.create]], validators[attributes={}; value=[validator[attributes={name=, type=class}; value=[arg[attributes={name=permission}; value=[Create Issue]], arg[attributes={name=class.name}; value=[com.atlassian.jira.workflow.validator.PermissionValidator]]]]]], results[attributes={}; value=[unconditional-result[attributes={old-status=null, status=open, step=1}; value=[post-functions[attributes={}; value=[function[attributes={type=class}; value=[arg[attributes={name=class.name}; value=[com.atlassian.jira.workflow.function.issue.IssueCreateFunction]]]], function[attributes={type=class}; value=[arg[attributes={name=class.name}; value=[com.atlassian.jira.workflow.function.issue.IssueReindexFunction]]]], function[attributes={type=class}; value=[arg[attributes={name=eventTypeId}; value=[1]], arg[attributes={name=class.name}; value=[com.atlassian.jira.workflow.function.event.FireIssueEventFunction]]]]]]]]]]]]] 
... 

所需的輸出: 獲取所有屬性和值,優選地在一些格式是這樣的:

attribute name : value 

jira.updated.date : 1503954892468 

jira.update.author.key : admin 
+0

什麼問題完全一樣。顯示的輸出是否正確?如果不是,那麼期望的輸出是什麼。 – Rao

+0

我在底部添加了一個部分;只是獲取屬性名稱和相關的值。問題不在於輸出值不正確,只是我無法以容易閱讀的格式(而不是按照數組)來顯示它們,並顯示屬性和相應的值。 –

+0

你只想要所有''元素的屬性嗎? – Rao

回答

0

的下面腳本將 -

  • 找到一個meta元素
  • name屬性創建地圖
  • 最後打印地圖。

不包括xml字符串,以避免大空間,無論如何你有它。

def metas = new XmlSlurper().parseText(xml).'**'.findAll {it.name() == 'meta'} 
def metaAttributeMap = metas.collectEntries{ [([email protected]):it.text()]} 
metaAttributeMap.each {k,v -> println "$k : $v"} 

您可以快速地嘗試相同的在線demo

+0

謝謝你的幫助。這隻能得到元素。是否有另一種使用通配符返回所有具有屬性和值的元素的方法?有些情況下,不清楚屬性會提前。 例如: