【问题标题】:add same tag Element to Element将相同的标签元素添加到元素
【发布时间】:2019-10-09 13:49:34
【问题描述】:

您好,我正在尝试将元素添加到解析为 Element 对象的 xml 文件中。 元素具有相同的标签并且处于同一级别。

正如您在输出中看到的,只有一个元素 intallationis append 我希望能够随心所欲地附加。

感谢您的帮助。

这是我要修改的 xml 文件

'''xml
<mesures>
    <mesure>
        <refMesure>LEM180895</refMesure>
        <conditions interieur="true" perimetreSecurite="false" champLointain="true">
            <duree dateDebut="2018-07-24" heureDebut="10:00" dateFin="2018-07-24" heureFin="12:30"/>
            <typeEnvironnement>PUBLIC</typeEnvironnement>
            <complement></complement>
        </conditions>
        <localisation changementAdresse="false">
            <motifChangementAdresse/>
            <position lon="2.3227499999999996" lat="48.8665556" hauteurSol="0"/>
            <adresse>
                <voie></voie>
                <lieudit></lieudit>
                <complement>string</complement>
                <codePostal>00000</codePostal>
                <commune>Commune</commune>

            </adresse>

        </localisation>
        <installationsVisibles>

        </installationsVisibles>
        <resultat respectNiveauReference="false">
            <valeurMoyenne>0</valeurMoyenne>
            <!--You have a CHOICE of the next 3 items at this level-->
            <!--Optional:-->
        </resultat>
    </mesure>
</mesures>
'''

这是我使用的代码

from lxml import etree
from lxml import objectify


fileobject = open('C:/Users/gustu/Dropbox/RATP2/uploadANFR/ANFRAutoXML/3 - Mise à jour schéma echange ANFR/Schema_Echanges_MCR/anfr_laboratoire/aideInternet/extract.xml','r')
tree = objectify.parse(fileobject)
root=tree.getroot()




# xml='''
# <installationVisibles>
# </installationVisibles>
# ''' 

# installationVisibles = objectify.fromstring(xml)




installation=objectify.fromstring("<installation hauteur=\"8\" distance=\"9\"><type></type></installation>")

root.mesure.installationsVisibles.insert(0,installation)
root.mesure.installationsVisibles.insert(1,installation)
print(etree.tostring(root))

<mesures><mesure><refMesure>LEM180895</refMesure><conditions 
interieur="true" perimetreSecurite="false" champLointain="true"><duree dateDebut="2018-07-24" heureDebut="10:00" dateFin="2018-07-24" heureFin="12:30"/><typeEnvironnement>PUBLIC</typeEnvironnement><complement/></conditions><localisation changementAdresse="false"><motifChangementAdresse/><position lon="2.3227499999999996" lat="48.8665556" hauteurSol="0"/><adresse><voie/><lieudit/><complement>string</complement><codePostal>00000</codePostal><commune>Commune</commune></adresse></localisation><installationsVisibles>\n\n        <installation hauteur="8" distance="9"><type/></installation></installationsVisibles><resultat respectNiveauReference="false"><valeurMoyenne>0</valeurMoyenne><!--You have a CHOICE of the next 3 items at this level--><!--Optional:--></resultat></mesure></mesures>

输出

<mesures>
    <mesure>
        <refMesure>LEM180895</refMesure>
        <conditions interieur="true" perimetreSecurite="false" champLointain="true">
            <duree dateDebut="2018-07-24" heureDebut="10:00" dateFin="2018-07-24" heureFin="12:30"/>
            <typeEnvironnement>PUBLIC
            </typeEnvironnement>
            <complement/>
        </conditions>
        <localisation changementAdresse="false">
            <motifChangementAdresse/>
            <position lon="2.3227499999999996" lat="48.8665556" hauteurSol="0"/>
            <adresse>
                <voie/>
                <lieudit/>
                <complement>string</complement>
                <codePostal>00000</codePostal>
                <commune>Commune</commune>
            </adresse>
        </localisation>
        <installationsVisibles>\n\n        
            <installation hauteur="8" distance="9">
                <type/>
            </installation>
        </installationsVisibles>
        <resultat respectNiveauReference="false">
            <valeurMoyenne>0</valeurMoyenne>            <!--You have a CHOICE of the next 3 items at this level-->            <!--Optional:--></resultat>
    </mesure>
</mesures>

【问题讨论】:

    标签: python tree lxml


    【解决方案1】:

    请补充

    from copy import copy
    

    替换

    root.mesure.installationsVisibles.insert(0, installation)
    root.mesure.installationsVisibles.insert(1, installation)
    

    与

    root.mesure.installationsVisibles.insert(0, copy(installation))
    root.mesure.installationsVisibles.insert(1, copy(installation))
    

    那么它应该可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-04
      • 2021-11-25
      • 2022-08-18
      • 2019-01-09
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多