使用xml元素中的文本的简单Python方法

2024-05-03 13:21:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我非常熟悉python/xml/编码的天赋让我整天都在尝试做一些我认为应该非常简单的事情,但没有成功。你知道吗

我已经成功地向服务器发送了一个XML请求,服务器生成了一个我的脚本似乎正在接收的响应,但是我想从返回的SessionID元素中获取文本响应,并将其粘贴到一个变量中,以便以后使用。你知道吗

似乎不管我怎么努力,我都看不懂那篇课文。你知道吗

我已经尽了最大的努力去了解https://docs.python.org/2/library/xml.etree.elementtree.html的文档,但是我运气不太好。你知道吗

我确信这是一件愚蠢的事情,但我遇到了困难,我希望有人能给我一些指导,让我找到一个简单的方法,去我想去的地方。你知道吗

$猫脚本.py你知道吗

#!/usr/bin/python
import socket
import time
import xml.etree.ElementTree as ET

auth_request = '''\
<?xml version="1.0" encoding="UTF-8" ?>
<OSS xmlns="http://www.zhone.com/OSSXML" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.zhone.com/OSSXML ossxml.xsd">
        <Request>
                <RequestType>authenticate</RequestType>
        </Request>
        <RequestElement>
                <Attribute>
                        <Name>loginName</Name>
                        <Value>boss</Value>
                </Attribute>
                <Attribute>
                        <Name>password</Name>
                        <Value>snaky79!OCAS</Value>
                </Attribute>
        </RequestElement>
</OSS>
'''
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("10.0.0.1", 15002))
s.send(auth_request)
buff = s.recv(400)
auth_response = ET.fromstring(buff)

print "Attempt 1:"
print auth_response.findall("*/SessionID")

print "Attempt 2:"
for a in auth_response.findall("*/status"):
        for b in a:
                print b
#print "Attempt 3:"
#c = auth_response.findall("*/status")
#for a in c:
#       print a.name
print "Attempt 4:"
print auth_response.getiterator()

print "Socket buffer:"
print buff

美元/脚本.py你知道吗

Attempt 1:
[<Element 'SessionID' at 0x1012a4d50>]
Attempt 2:
Attempt 4:
[<Element 'Response' at 0x1012a4950>, <Element 'OverAllStatus' at 0x1012a4990>, <Element 'OverAllDescription' at 0x1012a49d0>, <Element 'ResponseElement' at 0x1012a4a90>, <Element 'status' at 0x1012a4b50>, <Element 'Description' at 0x1012a4cd0>, <Element 'SessionID' at 0x1012a4d50>, <Element 'operName' at 0x1012a4d90>, <Element 'groupId' at 0x1012a4dd0>]
Socket buffer:
<Response>
<OverAllStatus>Success</OverAllStatus>
<OverAllDescription>The operation completed successfully</OverAllDescription>
<ResponseElement>
<status>Success</status>
<Description>Successfully Authenticated</Description>
<SessionID>0.379081153249641</SessionID>
<operName>boss</operName>
<groupId>99999</groupId>
</ResponseElement>
</Response>

FWIW,这是MacOS High Sierra上的工厂版Python2.7.10。你知道吗

谢谢!你知道吗


Tags: nameimport脚本authvalueresponsestatusattribute
2条回答

问题是在模式*/SessionID中,*组件只下降一级。如果正确格式化XML以显示结构

<Response>
    <OverAllStatus>Success</OverAllStatus>
    <OverAllDescription>The operation completed successfully</OverAllDescription>
    <ResponseElement>
        <status>Success</status>
        <Description>Successfully Authenticated</Description>
        <SessionID>0.379081153249641</SessionID>
        <operName>boss</operName>
        <groupId>99999</groupId>
    </ResponseElement>
</Response>

您会注意到,statusSessionID都嵌套在两个层次的深处,因此这两种模式都不起作用。最简单的解决方案是使用XPath语法

findall("//SessionID")

这将找到所有SessionID元素,不管它们嵌套得有多深。你知道吗

对于解析xml,可以使用xmltodict将得到的xml响应转换为dictionary,然后可以轻松地检索任何键的值,即SessionId。你知道吗

import xmltodict 

response = xmltodict.parse(buff)

print response["Response"]["ResponseElement"]["SessionID"]

这会给你

0.379081153249641

相关问题 更多 >