如何迭代多个xml文件,解析它们并发送到RESTAPI

如何迭代多个xml文件,解析它们并发送到RESTAPI,xml,python-3.x,file,xml-parsing,elementtree,Xml,Python 3.x,File,Xml Parsing,Elementtree,我已经创建了其余消息。我已经找到了如何解析单个文件的XML 我搞不懂的是如何解析目录中的多个文件。换句话说,我希望遍历每个文件,解析XML,并将XML中的某些元素传递到REST post消息中 在过去的两天里,我尝试了在互联网上能找到的一切,搜索和尝试。似乎什么都没用,或者我只是做错了…:\ 在我的评论中,我解释了我认为正在发生的事情。你可以在我给文件名的路径中看到我在说什么,解析xml文件并打印标签/文本。。正如所写的,它确认了我实际上有六个对象,这就是目录中有多少个*.xml文件。然后它打印

我已经创建了其余消息。我已经找到了如何解析单个文件的XML

我搞不懂的是如何解析目录中的多个文件。换句话说,我希望遍历每个文件,解析XML,并将XML中的某些元素传递到REST post消息中

在过去的两天里,我尝试了在互联网上能找到的一切,搜索和尝试。似乎什么都没用,或者我只是做错了…:\

在我的评论中,我解释了我认为正在发生的事情。你可以在我给文件名的路径中看到我在说什么,解析xml文件并打印标签/文本。。正如所写的,它确认了我实际上有六个对象,这就是目录中有多少个*.xml文件。然后它打印其中一个的所有元素和文本,实际上是中间的第四个文件

这是我看到的精确输出,减去一些敏感数据

import os
from xml.etree import ElementTree as ET
# files are in a sub folder where this script is being ran
path = "attachments"
for filename in os.listdir(path):
    # Only get xml files
    if not filename.endswith('.xml'): continue
    # I haven't been able to get it to work by just say
    fullname = os.path.join(path, filename)
    # This joins the path for each file it files so that python knows t
    tree = ET.parse(fullname)
    # Parse the files..
    print(tree)
    # Get the root of the XML tree structure
    root = tree.getroot()
    # Print the tags it finds from all the child elements from root
for child in root:
    print(child.tag, child.text)

缩进错误,多亏了Jack Fleeting。

您的for循环可能缩进错误。尝试将其缩进到与root=tree.getroot相同的行中。这就是问题所在!谢谢你,杰克·弗利廷!很高兴它对你有用@杰克·弗利廷,我怎么才能相信你呢?我想确保你得到答案。。。我不知道怎么做。显然我对Stack还不太熟悉。我的大部分开发生涯都是在ServiceNow JavaScript中度过的。刚刚开始扩展。别担心——下次你看到我的问题或答案时,请向上投票:
<xml.etree.ElementTree.ElementTree object at 0x0000018EF60E7608>
<xml.etree.ElementTree.ElementTree object at 0x0000018EF60DFE08>
<xml.etree.ElementTree.ElementTree object at 0x0000018EF62B1B08>
<xml.etree.ElementTree.ElementTree object at 0x0000018EF62E3F48>
<xml.etree.ElementTree.ElementTree object at 0x0000018EF629B608>
<xml.etree.ElementTree.ElementTree object at 0x0000018EF62B6988>

NUMBER 20514218
PARENT

STATUS 1-CLOSED
OPEN_DATE 12/01/2017 00:34:35
CLOSE_DATE 12/05/2017 17:48:28
SOURCE Self Service
PROCESS HR INTERNAL REQUEST FORM
CATEGORY HR Connect
SUB_CATEGORY Personnel Action Change/Update
USER_ID *sensitive information*
LAST_NAME *sensitive information*
FIRST_NAME Brandon
SITUATION SELECT...
PRIORITY 5 Days
ADVISOR_NAME ROMAN *sensitive information*
TEAM *sensitive information*
NEXT_ACTION

PROCESS_STATUS Verified
TRANSFERT_DATE

DEADLINE 12/12/2017 17:18:03
QUEUE HR Internal Request
FROZEN_DATE

OTHER_EMPLOYEE_ID *sensitive information*
REQUEST *sensitive information*

HISTORY_RESPONSE *sensitive information*
FINAL_RESPONSE *sensitive information*

-------------------here's the raw XML----------------------
<?xml version="1.0" encoding="UTF-8"?>
<CASE>
  <NUMBER>20514218</NUMBER>
  <PARENT>
  </PARENT>
  <STATUS>1-CLOSED</STATUS>
  <OPEN_DATE>12/01/2017 00:34:35</OPEN_DATE>
  <CLOSE_DATE>12/05/2017 17:48:28</CLOSE_DATE>
  <SOURCE>Self Service</SOURCE>
  <PROCESS>HR INTERNAL REQUEST FORM</PROCESS>
  <CATEGORY>HR Connect</CATEGORY>
  <SUB_CATEGORY>Personnel Action Change/Update</SUB_CATEGORY>
  <USER_ID>*sensitive information*</USER_ID>
  <LAST_NAME>*sensitive information*</LAST_NAME>
  <FIRST_NAME>*sensitive information*</FIRST_NAME>
  <SITUATION>SELECT...</SITUATION>
  <PRIORITY>5 Days</PRIORITY>
  <ADVISOR_NAME>ROMAN *sensitive information*</ADVISOR_NAME>
  <TEAM>2 HR SRV CNTR PA</TEAM>
  <NEXT_ACTION>
  </NEXT_ACTION>
  <PROCESS_STATUS>Verified</PROCESS_STATUS>
  <TRANSFERT_DATE>
  </TRANSFERT_DATE>
  <DEADLINE>12/12/2017 17:18:03</DEADLINE>
  <QUEUE>HR Internal Request</QUEUE>
  <FROZEN_DATE>
  </FROZEN_DATE>
  <OTHER_EMPLOYEE_ID>*sensitive information*</OTHER_EMPLOYEE_ID>
  <REQUEST>*sensitive information*</REQUEST>
  <HISTORY_RESPONSE>*sensitive information*</HISTORY_RESPONSE>
  <FINAL_RESPONSE>*sensitive information*</FINAL_RESPONSE>
</CASE>
import os
from xml.etree import ElementTree as ET
# files are in a sub folder where this script is being ran
path = "attachments"
for filename in os.listdir(path):
    # Only get xml files
    if not filename.endswith('.xml'): continue
    # I haven't been able to get it to work by just saying 'if filename.endswith('.xml')' only if not..
    fullname = os.path.join(path, filename)
    # This joins the path for each file it files so that python knows the full path / filename to trigger parser
    tree = ET.parse(fullname)
    # Parse the files..
    print(tree)
    # Get the root of the XML tree structure
    root = tree.getroot()
    # Print the tags it finds from all the child elements from root
    for child in root:
        print(child.tag, child.text)