使用lxml python更新xml文件

时间:2018-06-02 16:04:26

标签: xml python-3.x lxml

我有一个XML文件,其中包含会议室可用性详细信息。 我已经编写了应用程序来读取该xml文件并将信息存储到两个词典中,一个用于大型会议室,另一个用于小型会议室。我的任务是更新" isMeetingRoomAvailable"请求模式的xml参数。

对于Eg: 对于TowerName = tower one,meetingRoomName = ABC和MeetingRoomTimeStamp =" 2018-05-31 00:45:00",我必须将isMeetingRoomAvailable值更新为1.

以下是我写的代码:

from lxml import etree

root = etree.parse('test.xml')
bigRoomAvailabilityDict = {}
smallRoomAvailabilityDict = {}
towerNames =  root.xpath("/root/Towers/Tower/TowerName")
MeetingRoomNames = root.xpath("/root/Towers/Tower/MeetingRooms/BigMeetingRooms/MeetingRoom/MeetingRoomName")
for tower in root.xpath("/root/Towers/Tower"):
    towerName = tower.xpath("TowerName")[0].text
    for lr in tower.xpath("MeetingRooms/BigMeetingRooms/MeetingRoom"):
        meetingRoomName = lr.xpath("MeetingRoomName")[0].text
        MeetingRoomMailId = lr.xpath("MeetingRoomMailId")[0].text

        str = towerName + " " + meetingRoomName + " " + MeetingRoomMailId
        for timeAvai in lr.xpath("MeetingRoomAvailabilityInfo"):
            time = timeAvai.xpath("MeetingRoomTimeStamp")[0].text
            isAvai = timeAvai.xpath("isMeetingRoomAvailable")[0].text
            key = str + " " + time
            bigRoomAvailabilityDict[key] = isAvai

    for small in tower.xpath("MeetingRooms/SmallMeetingRooms/MeetingRoom"):
        meetingRoomName = small.xpath("MeetingRoomName")[0].text
        MeetingRoomMailId = small.xpath("MeetingRoomMailId")[0].text

        str = towerName + " " + meetingRoomName + " " + MeetingRoomMailId
        for timeAvai in small.xpath("MeetingRoomAvailabilityInfo"):
            time = timeAvai.xpath("MeetingRoomTimeStamp")[0].text
            isAvai = timeAvai.xpath("isMeetingRoomAvailable")[0].text
            key = str + " " + time
            smallRoomAvailabilityDict[key] = isAvai
def updateXML(towerName, meetinRoomName, timeStampVal):
    roomsFromXml = root.xpath("/root/Towers/Tower/MeetingRooms/BigMeetingRooms/MeetingRoom")
    # print(roomsFromXml)
    for index in roomsFromXml:
        roomNames = index.xpath("MeetingRoomName")
        for rooms in roomNames:
            # print(rooms.text)
            if rooms.text == meetinRoomName:
                allTimeStamps = index.xpath("MeetingRoomAvailabilityInfo")
                for timeStamps in allTimeStamps:
                    counter = 0
                    for myTime in timeStamps.xpath("MeetingRoomTimeStamp"):
                        counter += 1
                        if myTime.text == timeStampVal:
                            break
                    for isAvai in timeStamps.xpath("isMeetingRoomAvailable"):
                        counter -= 1
                        if counter == 0:
                            isAvai.text = 1

# for key, value in bigRoomAvailabilityDict.items():
#     print(key, "  " , value)

来自链接的XML文件......

<root>
  <Towers>
    <Tower>
      <TowerName>Tower one</TowerName>
      <MeetingRooms>
        <BigMeetingRooms>
          <MeetingRoom>
            <MeetingRoomName>ABC</MeetingRoomName>
            <MeetingRoomMailId>mail_id_ABC</MeetingRoomMailId>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:15:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:30:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 01:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 01:15:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 01:30:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 01:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 02:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 02:15:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 02:30:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 02:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 03:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 03:15:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 03:30:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 03:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 04:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 04:15:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 04:30:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 04:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 05:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
          </MeetingRoom>
          <MeetingRoom>
            <MeetingRoomName>JKL</MeetingRoomName>
            <MeetingRoomMailId>mail_id_JKL</MeetingRoomMailId>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:15:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:30:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 01:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 01:15:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 01:30:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 01:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 02:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 02:15:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 02:30:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 02:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 03:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 03:15:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 03:30:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 03:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 04:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 04:15:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 04:30:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 04:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 05:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
          </MeetingRoom>
        </BigMeetingRooms>
        <SmallMeetingRooms>
          <MeetingRoom>
            <MeetingRoomName>XYZ</MeetingRoomName>
            <MeetingRoomMailId>mail_id_XYZ</MeetingRoomMailId>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:15:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:30:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 01:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 01:15:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 01:30:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 01:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 02:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 02:15:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 02:30:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 02:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 03:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 03:15:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 03:30:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 03:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 04:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 04:15:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 04:30:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 04:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 05:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
          </MeetingRoom>
        </SmallMeetingRooms>
      </MeetingRooms>
    </Tower>
  </Towers>
</root>

1 个答案:

答案 0 :(得分:1)

如果我们只关注更新XML,我会做的是将树作为参数传递并传递包含其他值的dict。

您可以在xpath谓词中使用这些值来缩小要更新的元素的范围。这应该比使用for循环测试元素值简单得多。

示例...

来自问题的参数(将tower one更改为Tower one以匹配XML)

  

对于Eg:对于TowerName = Tower one,meetingRoomName = ABC和for   MeetingRoomTimeStamp =&#34; 2018-05-31 00:45:00&#34;,我必须更新   isMeetingRoomAvailable值为1。

XML输入(test.xml)(修改为小于原始版本)

<root>
  <Towers>
    <Tower>
      <TowerName>Tower one</TowerName>
      <MeetingRooms>
        <BigMeetingRooms>
          <MeetingRoom>
            <MeetingRoomName>ABC</MeetingRoomName>
            <MeetingRoomMailId>mail_id_ABC</MeetingRoomMailId>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 01:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
          </MeetingRoom>
          <MeetingRoom>
            <MeetingRoomName>JKL</MeetingRoomName>
            <MeetingRoomMailId>mail_id_JKL</MeetingRoomMailId>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
          </MeetingRoom>
        </BigMeetingRooms>
        <SmallMeetingRooms>
          <MeetingRoom>
            <MeetingRoomName>XYZ</MeetingRoomName>
            <MeetingRoomMailId>mail_id_XYZ</MeetingRoomMailId>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
          </MeetingRoom>
        </SmallMeetingRooms>
      </MeetingRooms>
    </Tower>
  </Towers>
</root>

Python 3.6

from lxml import etree
import sys

tower_name = "Tower one"
meeting_room = "ABC"
timestamp = "2018-05-31 00:45:00"


def update_xml(to_update, params):
    try:
        node = to_update.xpath("/root/Towers/Tower[TowerName='{}']/MeetingRooms/*/"
                               "MeetingRoom[MeetingRoomName='{}']/"
                               "MeetingRoomAvailabilityInfo[MeetingRoomTimeStamp='{}']/isMeetingRoomAvailable"
                               .format(params.get("TowerName"), params.get("MeetingRoom"), params.get("timestamp")))[0]
    except IndexError:
        sys.exit("cant find the xpath to update")
    else:
        node.text = "1"


tree = etree.parse("test.xml")
update_xml(tree, {"TowerName": tower_name, "MeetingRoom": meeting_room, "timestamp": timestamp})

print(etree.tostring(tree, encoding="unicode", pretty_print=True))

输出(来自打印)

<root>
  <Towers>
    <Tower>
      <TowerName>Tower one</TowerName>
      <MeetingRooms>
        <BigMeetingRooms>
          <MeetingRoom>
            <MeetingRoomName>ABC</MeetingRoomName>
            <MeetingRoomMailId>mail_id_ABC</MeetingRoomMailId>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:45:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>1</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 01:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
          </MeetingRoom>
          <MeetingRoom>
            <MeetingRoomName>JKL</MeetingRoomName>
            <MeetingRoomMailId>mail_id_JKL</MeetingRoomMailId>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
          </MeetingRoom>
        </BigMeetingRooms>
        <SmallMeetingRooms>
          <MeetingRoom>
            <MeetingRoomName>XYZ</MeetingRoomName>
            <MeetingRoomMailId>mail_id_XYZ</MeetingRoomMailId>
            <MeetingRoomAvailabilityInfo>
              <MeetingRoomTimeStamp>2018-05-31 00:00:00</MeetingRoomTimeStamp>
              <isMeetingRoomAvailable>0</isMeetingRoomAvailable>
            </MeetingRoomAvailabilityInfo>
          </MeetingRoom>
        </SmallMeetingRooms>
      </MeetingRooms>
    </Tower>
  </Towers>
</root>