The following is the root tag of the XML. When I parse this XML file using xml.etree.ElementTree
, I'm unable to retain the attributes with xmlns
prefix.
<ecore:EPackage xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI"
xmlns:ComIbmCompute.msgnode="ComIbmCompute.msgnode"
xmlns:ComIbmWSInput.msgnode="ComIbmWSInput.msgnode"
xmlns:ComIbmWSReply.msgnode="ComIbmWSReply.msgnode"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
xmlns:eflow="http://www.ibm.com/wbi/2005/eflow"
xmlns:utility="http://www.ibm.com/wbi/2005/eflow_utility"
nsURI="POC_UDP_ESQL.msgflow"
nsPrefix="POC_UDP_ESQL.msgflow">
I tried registering namespace using register_namespace
method in Python, then I'm able retain namespaces which I'm using in the xml (ecore:EPackage
) and unable to retain the other attributes (xmlns:ComIbmCompute.msgnode="ComIbmCompute.msgnode"
) which I'm not using in the xml.
XML output I'm getting:
<ecore:EPackage xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
xmlns:xmi="http://www.omg.org/XMI" xmi:version="2.0" nsURI="POC_UDP_ESQL.msgflow" nsPrefix="POC_UDP_ESQL.msgflow">
<eClassifiers xmi:type="eflow:FCMComposite" name="FCMComposite_1" nodeLayoutStyle="RECTANGLE">
<eSuperTypes href="http://www.ibm.com/wbi/2005/eflow#//FCMBlock"/>
<eStructuralFeatures xmi:type="ecore:EAttribute" xmi:id="Property.id" name="id" lowerBound="1" defaultValueLiteral="1">
<eType xmi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
</eStructuralFeatures>
<eStructuralFeatures xmi:type="ecore:EAttribute" xmi:id="Property.value" name="value" lowerBound="1" defaultValueLiteral="esql">
<eType xmi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eStructuralFeatures>
<composition>
<nodes xmi:type="ComIbmWSInput.msgnode:FCMComposite_1" xmi:id="FCMComposite_1_1" location="176,238" URLSpecifier="/test/udp/esql/1">
<translation xmi:type="utility:ConstantString" string="HTTP Input"/>
</nodes>
<nodes xmi:type="ComIbmWSReply.msgnode:FCMComposite_1" xmi:id="FCMComposite_1_2" location="781,234">
<translation xmi:type="utility:ConstantString" string="HTTP Reply"/>
</nodes>
<nodes xmi:type="ComIbmCompute.msgnode:FCMComposite_1" xmi:id="FCMComposite_1_3" location="465,233" computeExpression="esql://routine/#POC_Using_ESQL_Compute.Main">
<translation xmi:type="utility:ConstantString" string="Compute"/>
</nodes>
<connections xmi:type="eflow:FCMConnection" xmi:id="FCMConnection_1" targetNode="FCMComposite_1_3" sourceNode="FCMComposite_1_1" sourceTerminalName="OutTerminal.out" targetTerminalName="InTerminal.in"/>
<connections xmi:type="eflow:FCMConnection" xmi:id="FCMConnection_2" targetNode="FCMComposite_1_2" sourceNode="FCMComposite_1_3" sourceTerminalName="OutTerminal.out" targetTerminalName="InTerminal.in"/>
</composition>
<stickyBoard/>
</eClassifiers>
</ecore:EPackage>
- Input and the expected output xml(Just trying to parse the input and write into a file):
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI"
xmlns:ComIbmCompute.msgnode="ComIbmCompute.msgnode"
xmlns:ComIbmWSInput.msgnode="ComIbmWSInput.msgnode"
xmlns:ComIbmWSReply.msgnode="ComIbmWSReply.msgnode"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
xmlns:eflow="http://www.ibm.com/wbi/2005/eflow"
xmlns:utility="http://www.ibm.com/wbi/2005/eflow_utility" nsURI="POC_UDP_ESQL.msgflow" nsPrefix="POC_UDP_ESQL.msgflow">
<eClassifiers xmi:type="eflow:FCMComposite" name="FCMComposite_1" nodeLayoutStyle="RECTANGLE">
<eSuperTypes href="http://www.ibm.com/wbi/2005/eflow#//FCMBlock"/>
<eStructuralFeatures xmi:type="ecore:EAttribute" xmi:id="Property.id" name="id" lowerBound="1" defaultValueLiteral="1">
<eType xmi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
</eStructuralFeatures>
<eStructuralFeatures xmi:type="ecore:EAttribute" xmi:id="Property.value" name="value" lowerBound="1" defaultValueLiteral="esql">
<eType xmi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eStructuralFeatures>
<composition>
<nodes xmi:type="ComIbmWSInput.msgnode:FCMComposite_1" xmi:id="FCMComposite_1_1" location="176,238" URLSpecifier="/test/udp/esql/1">
<translation xmi:type="utility:ConstantString" string="HTTP Input"/>
</nodes>
<nodes xmi:type="ComIbmWSReply.msgnode:FCMComposite_1" xmi:id="FCMComposite_1_2" location="781,234">
<translation xmi:type="utility:ConstantString" string="HTTP Reply"/>
</nodes>
<nodes xmi:type="ComIbmCompute.msgnode:FCMComposite_1" xmi:id="FCMComposite_1_3" location="465,233" computeExpression="esql://routine/#POC_Using_ESQL_Compute.Main">
<translation xmi:type="utility:ConstantString" string="Compute"/>
</nodes>
<connections xmi:type="eflow:FCMConnection" xmi:id="FCMConnection_1" targetNode="FCMComposite_1_3" sourceNode="FCMComposite_1_1" sourceTerminalName="OutTerminal.out" targetTerminalName="InTerminal.in"/>
<connections xmi:type="eflow:FCMConnection" xmi:id="FCMConnection_2" targetNode="FCMComposite_1_2" sourceNode="FCMComposite_1_3" sourceTerminalName="OutTerminal.out" targetTerminalName="InTerminal.in"/>
</composition>
<stickyBoard/>
</eClassifiers>
</ecore:EPackage>
- Python code:
import xml.etree.ElementTree as xml
messageFlowFile = xml.parse('sample.xml')
print(messageFlowFile.getroot())
xml.register_namespace('xmi','http://www.omg.org/XMI')
xml.register_namespace('ecore','http://www.eclipse.org/emf/2002/Ecore')
#xml.register_namespace('eflow','http://www.ibm.com/wbi/2005/eflow')
#xml.register_namespace('utility','http://www.ibm.com/wbi/2005/eflow_utility')
messageFlowRoot = messageFlowFile.getroot()
print(messageFlowRoot.attrib)
messageFlowFile.write('NewMessage.xml', encoding='UTF-8', xml_declaration=True)