Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.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 修改现有xml文件_Python_Xml - Fatal编程技术网

Python 修改现有xml文件

Python 修改现有xml文件,python,xml,Python,Xml,我需要修改现有的xml文件。实际文件如下所示: 现在,我如何检查此人是否已经有了职业?如果没有:我如何添加专业和职位? 非常感谢 您可以使用simple加载xml文件,然后可以执行任何操作,如修改节点值、删除节点、创建节点 在您的例子中,假设您有一个现有的文件“person.xml”,其url就是一个例子 “xmls/person.xml” 在php文件中编写以下代码,比如“person.php” //让我们从加载“person.xml”开始 $root=simplexml_load_文件(

我需要修改现有的xml文件。实际文件如下所示:

现在,我如何检查此人是否已经有了职业?如果没有:我如何添加专业和职位?
非常感谢

您可以使用simple加载xml文件,然后可以执行任何操作,如修改节点值、删除节点、创建节点 在您的例子中,假设您有一个现有的文件“person.xml”,其url就是一个例子 “xmls/person.xml” 在php文件中编写以下代码,比如“person.php”

//让我们从加载“person.xml”开始
$root=simplexml_load_文件(“xmls/person.xml”);
$person=$root->xpath(“/person[profession='professor']”);
对于($i=0;$i参考->参考->位置[0]->位置[0]=“12-122-3”;
}
$root->asXml(“xmls/person.xml”);

你可以修改任何你想要的位置

到目前为止你做了什么?您是否尝试过在Python教程中至少使用一种XML?我尝试过使用minidom。但它不起作用。我会再试一次…从长远来看,你可能会想和我一起工作。您可以按照教程进行操作。好的,我使用minidom进行了尝试(请参阅我的编辑)。但如果有可能使用lxml,我也会感兴趣;)我真的很抱歉,我不知道用python做这件事
<person id="1">
    <address>Fr.</address>
    <titles/>
    <firstname>Mary</firstname>
    <lastname>Müller</lastname>
    <gender>F</gender>
    <profession/>
    <references>
        <reference>
            <positions>
                <position>68-590-1</position>
                <position>68-590-2</position>
            </positions>
            <positions>
                <position>68-590-6</position>
                <position>68-590-7</position>
            </positions>
        </reference>
    </references>
</person>
<person id="1">
    <address>Fr.</address>
    <titles/>
    <firstname>Mary</firstname>
    <lastname>Müller</lastname>
    <gender>F</gender>
    <profession>professor</profession>
    <references>
        <reference>
            <positions>
                <position>12-122-3</position>
            </positions>
            <positions>
                <position>68-590-1</position>
                <position>68-590-2</position>
            </positions>
            <positions>
                <position>68-590-6</position>
                <position>68-590-7</position>
            </positions>
        </reference>
    </references>
</person>
from xml.dom import minidom

doc = minidom.parse('SAC-Jahrbuch_1895_mul-ner.xml')
person = doc.getElementsByTagName('person')
lastnames = doc.getElementsByTagName('lastname')
positions = doc.getElementsByTagName('position')
professions = doc.getElementsByTagName('profession')
for ln in lastnames:
        for pos in positions:
                if ln.firstChild.nodeValue == u"Müller" and pos.firstChild.nodeValue == "68-590-2":
                    print "right person"
//so let us begin with loading "person.xml"   
 $root = simplexml_load_file("xmls/person.xml");
 $person = $root->xpath("/person[profession='professor']");
 for($i = 0; $i < count($person); $i++) 
 {
   //i will modify just the first position and you can do this for the other
   $person[$i]->references->reference->positions[0]->position[0]="12-122-3";
 }

 $root->asXml("xmls/person.xml");