Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.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
XML在Python中未返回正确的子标记/数据_Python_Xml_Python Requests_Elementtree - Fatal编程技术网

XML在Python中未返回正确的子标记/数据

XML在Python中未返回正确的子标记/数据,python,xml,python-requests,elementtree,Python,Xml,Python Requests,Elementtree,你好,我正在打一个请求电话,要求从网上商店退回订单数据。我的问题是,一旦我将数据传递给根变量,iter方法就不会返回正确的结果。e、 g.显示多个相同名称的标签,而不是一个,并且不显示标签内的数据 我认为这是因为XML格式不正确,所以我使用pretty_print将其保存到一个文件中,对其进行了格式化,但这并没有修复错误 我该如何解决这个问题提前谢谢 代码: 订单输出: {http://publicapi.ekmpowershop.com/}Order {} {http://publicapi.

你好,我正在打一个请求电话,要求从网上商店退回订单数据。我的问题是,一旦我将数据传递给根变量,iter方法就不会返回正确的结果。e、 g.显示多个相同名称的标签,而不是一个,并且不显示标签内的数据

我认为这是因为XML格式不正确,所以我使用
pretty_print
将其保存到一个文件中,对其进行了格式化,但这并没有修复错误

我该如何解决这个问题提前谢谢

代码:

订单输出:

{http://publicapi.ekmpowershop.com/}Order {}
{http://publicapi.ekmpowershop.com/}Order {}
格式化后的XML结构:

 <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetOrdersResponse xmlns="http://publicapi.ekmpowershop.com/">
      <GetOrdersResult>
        <Status>Success</Status>
        <Errors/>
        <Date>2018-07-10T13:47:00.1682029+01:00</Date>
        <TotalOrders>10</TotalOrders>
        <TotalCost>100</TotalCost>
        <Orders>
          <Order>
            <OrderID>100</OrderID>
            <OrderNumber>102/040718/67</OrderNumber>
            <CustomerID>6910</CustomerID>
            <CustomerUserID>204</CustomerUserID>
            <FirstName>TestFirst</FirstName>
            <LastName>TestLast</LastName>
            <CompanyName>Test Company</CompanyName>
            <EmailAddress>test@Test.com</EmailAddress>
            <OrderStatus>Dispatched</OrderStatus>
            <OrderStatusColour>#00CC00</OrderStatusColour>
            <TotalCost>85.8</TotalCost>
            <OrderDate>10/07/2018 14:30:43</OrderDate>
            <OrderDateISO>2018-07-10T14:30:43</OrderDateISO>
            <AbandonedOrder>false</AbandonedOrder>
            <EkmStatus>SUCCESS</EkmStatus>
          </Order>
        </Orders>
        <Currency>GBP</Currency>
      </GetOrdersResult>
    </GetOrdersResponse>
  </soap:Body>
</soap:Envelope>

成功
2018-07-10T13:47:00.1682029+01:00
10
100
100
102/040718/67
6910
204
测试优先
试片
测试公司
test@Test.com
派遣
#00CC00
85.8
10/07/2018 14:30:43
2018-07-10T14:30:43
错误的
成功
英镑

<代码> > p>在检查标签时需要考虑命名空间。

>>> # Include the namespace part of the tag in the tag values that we check.
>>> tags = ('{http://publicapi.ekmpowershop.com/}OrderID', '{http://publicapi.ekmpowershop.com/}OrderNumber')
>>> for order in root.iter('{http://publicapi.ekmpowershop.com/}Order'):
...     out = {}
...     for child in order:
...         if child.tag in tags:
...             out[child.tag] = child.text
...     print(out)
... 
{'{http://publicapi.ekmpowershop.com/}OrderID': '100', '{http://publicapi.ekmpowershop.com/}OrderNumber': '102/040718/67'}
如果您不想在输出中使用名称空间前缀,可以通过仅在
}
字符后包含标记的该部分来去除它们

>>> for order in root.iter('{http://publicapi.ekmpowershop.com/}Order'):
...     out = {}
...     for child in order:
...         if child.tag in tags:
...             out[child.tag[child.tag.index('}')+1:]] = child.text
...     print(out)
... 
{'OrderID': '100', 'OrderNumber': '102/040718/67'}

预期的输出是什么?预期的输出应该是这样的:
Order{'OrderID':'1234','OrderNo':'1234'}
当我使用它时,我只会得到每个订单的空dict返回-我已经用你的循环更新了我的代码,并更改了变量以获得正确的名称。我做错了什么?Thanks@McRoland查看您正在解析的xml示例可能会有所帮助—您可以将其添加到问题中吗?当然,我已经将xml添加到了问题中now@McRoland我已经根据您提供的示例xml重写了答案。谢谢,这将帮助我加载!我只是学习了XML排序的教程,不知道如何修复它,这对我以后会有很大帮助!
>>> # Include the namespace part of the tag in the tag values that we check.
>>> tags = ('{http://publicapi.ekmpowershop.com/}OrderID', '{http://publicapi.ekmpowershop.com/}OrderNumber')
>>> for order in root.iter('{http://publicapi.ekmpowershop.com/}Order'):
...     out = {}
...     for child in order:
...         if child.tag in tags:
...             out[child.tag] = child.text
...     print(out)
... 
{'{http://publicapi.ekmpowershop.com/}OrderID': '100', '{http://publicapi.ekmpowershop.com/}OrderNumber': '102/040718/67'}
>>> for order in root.iter('{http://publicapi.ekmpowershop.com/}Order'):
...     out = {}
...     for child in order:
...         if child.tag in tags:
...             out[child.tag[child.tag.index('}')+1:]] = child.text
...     print(out)
... 
{'OrderID': '100', 'OrderNumber': '102/040718/67'}