在xml文件的根标记内添加内容

时间:2018-04-23 07:58:17

标签: python xml-parsing

我有一个现有的xml文件,想要添加一些内容。

现有的xml文件:

<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
    <edmx:Reference Uri="/redfish/v1/Schemas/Resource_v1.xml">
        <edmx:Include Namespace="Resource"/>
        <edmx:Include Namespace="Resource.v1_0_0"/>
        <edmx:Include Namespace="Resource.v1_0_2"/>
        <edmx:Include Namespace="Resource.v1_4_0"/>
    </edmx:Reference>
    <edmx:DataServices>
        <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Service">
            <EntityContainer Name="Service" Extends="ServiceRoot.v1_0_0.ServiceContainer"/>
        </Schema>
    </edmx:DataServices>
</edmx:Edmx>

现在我想添加以下内容而不修改现有文件的内容

我的问题是如何在不修改此标记内现有xml文件的内容的情况下将xml的内容添加到现有文件中:<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">

我尝试了这段代码:

new="""<edmx:Reference Uri="/redfish/v1/Schemas/Chassis_v1.xml">
<edmx:Include Namespace="Chassis"/>
<edmx:Include Namespace="Chassis.v1_0_0"/>
<edmx:Include Namespace="Chassis.v1_0_1"/>
</edmx:Reference>"""


tree = ET.parse('metadata.xml')
root = tree.getroot()

ET.register_namespace('edmx','http://docs.oasis-open.org/odata/ns/edmx')
ET.register_namespace('','http://docs.oasis-open.org/odata/ns/edm')
root.text = new

print ET.tostring(root,method="html")

但输出是:

<edmx:Edmx xmlns="http://docs.oasis-open.org/odata/ns/edm" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">&lt;edmx:Reference Uri="/redfish/v1/Schemas/Chassis_v1.xml"&gt;
&lt;edmx:Include Namespace="Chassis"/&gt;
&lt;edmx:Include Namespace="Chassis.v1_0_0"/&gt;
&lt;edmx:Include Namespace="Chassis.v1_0_1"/&gt;
&lt;/edmx:Reference&gt;<edmx:Reference Uri="/redfish/v1/Schemas/Resource_v1.xml">
        <edmx:Include Namespace="Resource"></edmx:Include>
        <edmx:Include Namespace="Resource.v1_0_0"></edmx:Include>
        <edmx:Include Namespace="Resource.v1_0_2"></edmx:Include>
        <edmx:Include Namespace="Resource.v1_4_0"></edmx:Include>
    </edmx:Reference>
    <edmx:DataServices>
        <Schema Namespace="Service">
            <EntityContainer Extends="ServiceRoot.v1_0_0.ServiceContainer" Name="Service"></EntityContainer>
        </Schema>
    </edmx:DataServices>
</edmx:Edmx>

1 个答案:

答案 0 :(得分:0)

由于您必须在parent / root属性中添加内容,因此此代码将添加您的内容:

新内容:

new="""<edmx:Reference Uri="/redfish/v1/Schemas/Chassis_v1.xml">
<edmx:Include Namespace="Chassis"/>
<edmx:Include Namespace="Chassis.v1_0_0"/>
<edmx:Include Namespace="Chassis.v1_0_1"/>
</edmx:Reference>"""

代码:

from xml.etree.ElementTree import parse, Element
xml_doc = parse('metadata.xml') #parse xml file here
root = xml_doc.getroot()  #get root element here...
root.append(Element(new)) #new element/attribute you want to get 
xml_doc.write('metadata.xml', xml_declaration=True)

输出metadata.xml如下:

<ns0:Edmx xmlns:ns0="http://docs.oasis-open.org/odata/ns/edmx" xmlns:ns1="http://docs.oasis-open.org/odata/ns/edm" Version="4.0">
    <ns0:Reference Uri="/redfish/v1/Schemas/Resource_v1.xml">
        <ns0:Include Namespace="Resource" />
        <ns0:Include Namespace="Resource.v1_0_0" />
        <ns0:Include Namespace="Resource.v1_0_2" />
        <ns0:Include Namespace="Resource.v1_4_0" />
    </ns0:Reference>
    <ns0:DataServices>
        <ns1:Schema Namespace="Service">
            <ns1:EntityContainer Extends="ServiceRoot.v1_0_0.ServiceContainer" Name="Service" />
        </ns1:Schema>
    </ns0:DataServices>
    <!-- added new content here -->
<<edmx:Reference Uri="/redfish/v1/Schemas/Chassis_v1.xml">
<edmx:Include Namespace="Chassis"/>
<edmx:Include Namespace="Chassis.v1_0_0"/>
<edmx:Include Namespace="Chassis.v1_0_1"/>
</edmx:Reference> /></ns0:Edmx>