讀取 XML 檔轉換後輸出

XML 檔案範例

<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test"> <process id="myProcess" name="My process" isExecutable="true"> <startEvent id="StartProcess" name="開始"></startEvent> <userTask id="CollectData1" name="蒐集資料-1"> <documentation>資料源位址</documentation> </userTask> <sequenceFlow id="flow1" sourceRef="StartProcess" targetRef="P"></sequenceFlow> <userTask id="FormatData" name="資料整理"> <documentation>資料暫存位址</documentation> </userTask> <scriptTask id="AnalysisProcedures11" name="相關係數分析" activiti:autoStoreVariables="false"> <documentation>自動化分析程序</documentation> <script>R</script> </scriptTask> <scriptTask id="ImportDB" name="匯入資料庫" scriptFormat="javascript" activiti:autoStoreVariables="false"> <documentation>資料庫</documentation> <script>MongoDB</script> </scriptTask> <sequenceFlow id="flow3" sourceRef="FormatData" targetRef="ImportDB"></sequenceFlow> <sequenceFlow id="flow4" sourceRef="AnalysisProcedures10" targetRef="AnalysisProcedures11"></sequenceFlow> <startEvent id="NotificationMessage" name="分析結果通知"> <messageEventDefinition></messageEventDefinition> </startEvent> <sequenceFlow id="flow5" name="發送訊息" sourceRef="AnalysisProcedures13" targetRef="NotificationMessage"></sequenceFlow> <userTask id="ExplainResult" name="解讀分析"> <documentation>撰寫分析報告</documentation> </userTask> <sequenceFlow id="flow6" name="通知" sourceRef="NotificationMessage" targetRef="ExplainResult"></sequenceFlow> <userTask id="TakeAction1" name="管理作為-1"> <documentation>作用者:作為:被作用者</documentation> </userTask> <serviceTask id="MeasurePerformance" name="績效評估"></serviceTask> <sequenceFlow id="flow9" sourceRef="TakeAction1" targetRef="MeasurePerformance"></sequenceFlow> <parallelGateway id="ParallelActivities" name="平行處理"></parallelGateway> <sequenceFlow id="flow11" name="採取作為" sourceRef="ExplainResult" targetRef="ParallelActivities"></sequenceFlow> <sequenceFlow id="flow12" sourceRef="ParallelActivities" targetRef="TakeAction1"></sequenceFlow> <userTask id="usertask1TakeAction2" name="管理作為-2"></userTask> <sequenceFlow id="flow13" sourceRef="ParallelActivities" targetRef="usertask1TakeAction2"></sequenceFlow> <sequenceFlow id="flow14" sourceRef="usertask1TakeAction2" targetRef="MeasurePerformance"></sequenceFlow> <userTask id="TakAction3" name="管理作為-3"></userTask> <sequenceFlow id="flow15" sourceRef="ParallelActivities" targetRef="TakAction3"></sequenceFlow> <sequenceFlow id="flow16" sourceRef="TakAction3" targetRef="MeasurePerformance"></sequenceFlow> <userTask id="CollectData2" name="蒐集資料-2"></userTask> <userTask id="CollectData3" name="蒐集資料-3"></userTask> <parallelGateway id="ParallelActivity0" name="平行處理-0"></parallelGateway> <sequenceFlow id="flow17" sourceRef="P" targetRef="CollectData1"></sequenceFlow> <sequenceFlow id="flow18" sourceRef="P" targetRef="CollectData2"></sequenceFlow> <sequenceFlow id="flow19" sourceRef="P" targetRef="CollectData3"></sequenceFlow> <sequenceFlow id="flow20" sourceRef="CollectData1" targetRef="AnalysisProcedures01"></sequenceFlow> <scriptTask id="AnalysisProcedures01" name="資料初步分析-1" activiti:autoStoreVariables="false"></scriptTask> <scriptTask id="AnalysisProcedures02" name="資料初步分析-2" activiti:autoStoreVariables="false"></scriptTask> <scriptTask id="AnalysisProcedures03" name="資料初步分析-3" activiti:autoStoreVariables="false"></scriptTask> <sequenceFlow id="flow21" sourceRef="CollectData2" targetRef="AnalysisProcedures02"></sequenceFlow> <sequenceFlow id="flow22" sourceRef="CollectData3" targetRef="AnalysisProcedures03"></sequenceFlow> <parallelGateway id="parallelgateway1" name="平行處理-0"></parallelGateway> <sequenceFlow id="flow23" sourceRef="AnalysisProcedures01" targetRef="parallelgateway1"></sequenceFlow> <sequenceFlow id="flow24" sourceRef="AnalysisProcedures02" targetRef="parallelgateway1"></sequenceFlow> <sequenceFlow id="flow25" sourceRef="AnalysisProcedures03" targetRef="parallelgateway1"></sequenceFlow> <sequenceFlow id="flow26" sourceRef="parallelgateway1" targetRef="FormatData"></sequenceFlow> <scriptTask id="AnalysisProcedures10" name="繪製散佈圖" activiti:autoStoreVariables="false"> <script>R</script> </scriptTask> <sequenceFlow id="flow27" sourceRef="ImportDB" targetRef="AnalysisProcedures10"></sequenceFlow> <scriptTask id="AnalysisProcedures12" name="回歸分析" activiti:autoStoreVariables="false"> <script>R</script> </scriptTask> <sequenceFlow id="flow28" sourceRef="AnalysisProcedures11" targetRef="AnalysisProcedures12"></sequenceFlow> <scriptTask id="AnalysisProcedures13" name="繪製回歸線性圖" activiti:autoStoreVariables="false"> <script>R</script> </scriptTask> <sequenceFlow id="flow29" sourceRef="AnalysisProcedures12" targetRef="AnalysisProcedures13"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess"> <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess"> <bpmndi:BPMNShape bpmnElement="StartProcess" id="BPMNShape_StartProcess"> <omgdc:Bounds height="35.0" width="35.0" x="75.0" y="127.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="CollectData1" id="BPMNShape_CollectData1"> <omgdc:Bounds height="55.0" width="105.0" x="224.0" y="56.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="FormatData" id="BPMNShape_FormatData"> <omgdc:Bounds height="55.0" width="105.0" x="604.0" y="117.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="AnalysisProcedures11" id="BPMNShape_AnalysisProcedures11"> <omgdc:Bounds height="55.0" width="105.0" x="752.0" y="296.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="ImportDB" id="BPMNShape_ImportDB"> <omgdc:Bounds height="55.0" width="105.0" x="752.0" y="117.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="NotificationMessage" id="BPMNShape_NotificationMessage"> <omgdc:Bounds height="35.0" width="35.0" x="526.0" y="306.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="ExplainResult" id="BPMNShape_ExplainResult"> <omgdc:Bounds height="55.0" width="105.0" x="387.0" y="384.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="TakeAction1" id="BPMNShape_TakeAction1"> <omgdc:Bounds height="55.0" width="105.0" x="225.0" y="255.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="MeasurePerformance" id="BPMNShape_MeasurePerformance"> <omgdc:Bounds height="55.0" width="105.0" x="69.0" y="319.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="ParallelActivities" id="BPMNShape_ParallelActivities"> <omgdc:Bounds height="40.0" width="40.0" x="419.0" y="262.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask1TakeAction2" id="BPMNShape_usertask1TakeAction2"> <omgdc:Bounds height="55.0" width="105.0" x="225.0" y="319.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="TakAction3" id="BPMNShape_TakAction3"> <omgdc:Bounds height="55.0" width="105.0" x="225.0" y="384.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="CollectData2" id="BPMNShape_CollectData2"> <omgdc:Bounds height="55.0" width="105.0" x="224.0" y="117.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="CollectData3" id="BPMNShape_CollectData3"> <omgdc:Bounds height="55.0" width="105.0" x="224.0" y="179.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="ParallelActivity0" id="BPMNShape_ParallelActivity0"> <omgdc:Bounds height="40.0" width="40.0" x="144.0" y="124.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="AnalysisProcedures01" id="BPMNShape_AnalysisProcedures01"> <omgdc:Bounds height="55.0" width="105.0" x="374.0" y="56.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="AnalysisProcedures02" id="BPMNShape_AnalysisProcedures02"> <omgdc:Bounds height="55.0" width="105.0" x="375.0" y="117.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="AnalysisProcedures03" id="BPMNShape_AnalysisProcedures03"> <omgdc:Bounds height="55.0" width="105.0" x="375.0" y="179.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="parallelgateway1" id="BPMNShape_parallelgateway1"> <omgdc:Bounds height="40.0" width="40.0" x="523.0" y="124.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="AnalysisProcedures10" id="BPMNShape_AnalysisProcedures10"> <omgdc:Bounds height="55.0" width="105.0" x="752.0" y="206.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="AnalysisProcedures12" id="BPMNShape_AnalysisProcedures12"> <omgdc:Bounds height="55.0" width="105.0" x="604.0" y="296.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="AnalysisProcedures13" id="BPMNShape_AnalysisProcedures13"> <omgdc:Bounds height="55.0" width="105.0" x="604.0" y="206.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"> <omgdi:waypoint x="709.0" y="144.0"></omgdi:waypoint> <omgdi:waypoint x="752.0" y="144.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"> <omgdi:waypoint x="804.0" y="261.0"></omgdi:waypoint> <omgdi:waypoint x="804.0" y="296.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5"> <omgdi:waypoint x="604.0" y="233.0"></omgdi:waypoint> <omgdi:waypoint x="543.0" y="233.0"></omgdi:waypoint> <omgdi:waypoint x="543.0" y="306.0"></omgdi:waypoint> <bpmndi:BPMNLabel> <omgdc:Bounds height="11.0" width="100.0" x="555.0" y="277.0"></omgdc:Bounds> </bpmndi:BPMNLabel> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6"> <omgdi:waypoint x="543.0" y="341.0"></omgdi:waypoint> <omgdi:waypoint x="543.0" y="410.0"></omgdi:waypoint> <omgdi:waypoint x="492.0" y="411.0"></omgdi:waypoint> <bpmndi:BPMNLabel> <omgdc:Bounds height="11.0" width="100.0" x="554.0" y="366.0"></omgdc:Bounds> </bpmndi:BPMNLabel> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9"> <omgdi:waypoint x="225.0" y="282.0"></omgdi:waypoint> <omgdi:waypoint x="121.0" y="282.0"></omgdi:waypoint> <omgdi:waypoint x="121.0" y="319.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow11" id="BPMNEdge_flow11"> <omgdi:waypoint x="439.0" y="384.0"></omgdi:waypoint> <omgdi:waypoint x="439.0" y="302.0"></omgdi:waypoint> <bpmndi:BPMNLabel> <omgdc:Bounds height="11.0" width="100.0" x="446.0" y="348.0"></omgdc:Bounds> </bpmndi:BPMNLabel> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow12" id="BPMNEdge_flow12"> <omgdi:waypoint x="419.0" y="282.0"></omgdi:waypoint> <omgdi:waypoint x="330.0" y="282.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow13" id="BPMNEdge_flow13"> <omgdi:waypoint x="439.0" y="302.0"></omgdi:waypoint> <omgdi:waypoint x="355.0" y="339.0"></omgdi:waypoint> <omgdi:waypoint x="330.0" y="346.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow14" id="BPMNEdge_flow14"> <omgdi:waypoint x="225.0" y="346.0"></omgdi:waypoint> <omgdi:waypoint x="174.0" y="346.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow15" id="BPMNEdge_flow15"> <omgdi:waypoint x="439.0" y="302.0"></omgdi:waypoint> <omgdi:waypoint x="356.0" y="398.0"></omgdi:waypoint> <omgdi:waypoint x="330.0" y="411.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow16" id="BPMNEdge_flow16"> <omgdi:waypoint x="225.0" y="411.0"></omgdi:waypoint> <omgdi:waypoint x="121.0" y="411.0"></omgdi:waypoint> <omgdi:waypoint x="121.0" y="374.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow20" id="BPMNEdge_flow20"> <omgdi:waypoint x="329.0" y="83.0"></omgdi:waypoint> <omgdi:waypoint x="374.0" y="83.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow21" id="BPMNEdge_flow21"> <omgdi:waypoint x="329.0" y="144.0"></omgdi:waypoint> <omgdi:waypoint x="375.0" y="144.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow22" id="BPMNEdge_flow22"> <omgdi:waypoint x="329.0" y="206.0"></omgdi:waypoint> <omgdi:waypoint x="375.0" y="206.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow23" id="BPMNEdge_flow23"> <omgdi:waypoint x="479.0" y="83.0"></omgdi:waypoint> <omgdi:waypoint x="543.0" y="83.0"></omgdi:waypoint> <omgdi:waypoint x="543.0" y="124.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow24" id="BPMNEdge_flow24"> <omgdi:waypoint x="480.0" y="144.0"></omgdi:waypoint> <omgdi:waypoint x="523.0" y="144.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow25" id="BPMNEdge_flow25"> <omgdi:waypoint x="480.0" y="206.0"></omgdi:waypoint> <omgdi:waypoint x="543.0" y="206.0"></omgdi:waypoint> <omgdi:waypoint x="543.0" y="164.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow26" id="BPMNEdge_flow26"> <omgdi:waypoint x="563.0" y="144.0"></omgdi:waypoint> <omgdi:waypoint x="604.0" y="144.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow27" id="BPMNEdge_flow27"> <omgdi:waypoint x="804.0" y="172.0"></omgdi:waypoint> <omgdi:waypoint x="804.0" y="206.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow28" id="BPMNEdge_flow28"> <omgdi:waypoint x="752.0" y="323.0"></omgdi:waypoint> <omgdi:waypoint x="709.0" y="323.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow29" id="BPMNEdge_flow29"> <omgdi:waypoint x="656.0" y="296.0"></omgdi:waypoint> <omgdi:waypoint x="656.0" y="261.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>

程式碼

# -*- coding: utf-8 -*- import sys import xml.etree.ElementTree as ET class Activity: def __init__(self): pass def getTagName(self, s): endPos = s.find('}') tag = s[endPos + 1:] return tag def loadFile(self, xmlFile): tree = ET.parse(xmlFile) root = tree.getroot() for child in root: if self.getTagName(child.tag) == 'process': for attr in child.attrib: if attr == 'name': print "%s:" % (child.attrib[attr]) for child2 in child: tagName2 = self.getTagName(child2.tag) if 'Task' in tagName2: print "\t%s:" % (tagName2) for attr2 in child2.attrib: if attr2 == 'name': print "\t\t%s=[%s]" % \ (attr2, child2.attrib[attr2]) for child3 in child2: print "\t\t%s=[%s]" % \ (self.getTagName(child3.tag), child3.text) if __name__ == '__main__': activity = Activity() if len(sys.argv) == 1: for iFile in sys.stdin: activity.loadFile(iFile.strip()) elif len(sys.argv) > 1: activity.loadFile(sys.argv[1])

輸出

My process: userTask: name=[蒐集資料-1] documentation=[資料源位址] userTask: name=[資料整理] documentation=[資料暫存位址] scriptTask: name=[相關係數分析] documentation=[自動化分析程序] script=[R] scriptTask: name=[匯入資料庫] documentation=[資料庫] script=[MongoDB] userTask: name=[解讀分析] documentation=[撰寫分析報告] userTask: name=[管理作為-1] documentation=[作用者:作為:被作用者] serviceTask: name=[績效評估] userTask: name=[管理作為-2] userTask: name=[管理作為-3] userTask: name=[蒐集資料-2] userTask: name=[蒐集資料-3] scriptTask: name=[資料初步分析-1] scriptTask: name=[資料初步分析-2] scriptTask: name=[資料初步分析-3] scriptTask: name=[繪製散佈圖] script=[R] scriptTask: name=[回歸分析] script=[R] scriptTask: name=[繪製回歸線性圖] script=[R]