Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向元素添加相同的标记元素_Python_Tree_Lxml - Fatal编程技术网

Python 向元素添加相同的标记元素

Python 向元素添加相同的标记元素,python,tree,lxml,Python,Tree,Lxml,您好,我正在尝试将元素添加到解析为元素对象的xml文件中。 元素具有相同的标记并且处于相同的级别 正如您在输出中所看到的那样,在Installationis append中只有一个元素,我希望能够按照自己的意愿进行追加 谢谢你的帮助 这是我试图修改的xml文件 '''xml <mesures> <mesure> <refMesure>LEM180895</refMesure> <conditions i

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

正如您在输出中所看到的那样,在Installationis 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>
'''
''xml
LEM180895
公开的
一串
00000
公社
0
'''
这是我使用的代码

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>
从lxml导入etree
从lxml导入objectify
fileobject=open('C:/Users/gustu/Dropbox/RATP2/uploadANFR/anfrautoml/3-Miseájour schéma echange ANFR/Schema\u Echanges\u MCR/ANFR\u laboratoroire/aideInternet/extract.xml','r')
tree=objectify.parse(fileobject)
root=tree.getroot()
#xml=“”
# 
# 
# ''' 
#installationVisibles=objectify.fromstring(xml)
安装=objectify.fromstring(“”)
root.mesure.installationsVisibles.insert(0,安装)
root.mesure.installationsVisibles.insert(1,安装)
打印(etree.tostring(根))
LEM180895PUBLICstring00000Commune\n\n 0
输出

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

LEM180895
公开的
一串
00000
公社
\n\n
0
请添加

从副本导入副本
替换

root.mesure.installationsVisibles.insert(0,安装)
root.mesure.installationsVisibles.insert(1,安装)

root.mesure.installationsVisibles.insert(0,复制(安装))
root.mesure.installationsVisibles.insert(1,副本(安装))
那么它应该会起作用