如何在python中解析xml时保留未使用的命名空间



以下是XML的根标记。当我使用xml.etree.ElementTree解析此XML文件时,我无法保留前缀为xmlns的属性。

<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">

我尝试在Python中使用register_namespace方法注册名称空间,然后我可以保留我在xml中使用的名称空间(ecore:EPackage(,而不能保留我在xml中没有使用的其他属性(xmlns:ComIbmCompute.msgnode="ComIbmCompute.msgnode"(。

我得到的XML输出:

<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>
  • 输入和期望的输出xml(只是试图解析输入和写入文件(:

<?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代码:

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)

通过使用lxml,我能够实现任务


from lxml import etree

tree = etree.parse('sample.xml')
#Making some modifications
tree.write('newMessage.xml')

最新更新