如何提取XML格式的数据?

2024-04-25 08:53:16 发布

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

我调用了这个服务,它以xml格式向我返回数据。我想从中提取服务器地址。我该怎么做?这是我从服务中心打电话时得到的。你知道吗

from xml.dom import minidom
import requests


url="http://172.10.3.2:51106/GetConnectionStrings.asmx"

#headers = {'content-type': 'application/soap+xml'}
headers = {'content-type': 'text/xml'}
body = """<?xml version='1.0' encoding='utf-8'?>
                            <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
                              <soap:Body>
                                <DatabaseConnectionString xmlns='http://tempuri.org/'>
                                  <DatabaseName>ELMA</DatabaseName>
                                  <ApplicationName>MonitoringSystem</ApplicationName>
                                </DatabaseConnectionString>
                              </soap:Body>
                            </soap:Envelope>"""

response = requests.post(url,data=body,headers=headers)
#print response.content
doc = minidom.parseString(response.content)

# doc.getElementsByTagName returns NodeList
name = doc.getElementsByTagName("DatabaseConnectionStringResult")[0]
print(name.firstChild.data)

这就是我目前所尝试的。你知道吗

Data Source=172.10.3.3;Initial Catalog=Elma;User ID=User11021969;Password=ILoveMyMOM;MultipleActiveResultSets=True;Min Pool Size=5;Max Pool Size=5000;Connect Timeout=180;Application Name=MonitoringSystem

我想提取数据源172.10.3.3并将其保存为字符串。你知道吗


Tags: orgimporthttpurldocresponsetypebody
1条回答
网友
1楼 · 发布于 2024-04-25 08:53:16

我不知道你所说的“数据源172.10.33”是什么意思,因为这个IP地址没有出现在你的正文的任何地方。你知道吗

要从文本体中搜索和提取信息,请使用正则表达式

body = """<?xml version='1.0' encoding='utf-8'?>
                        <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
                          <soap:Body>
                            <DatabaseConnectionString xmlns='http://tempuri.org/'>
                              <DatabaseName>ELMA</DatabaseName>
                              <ApplicationName>MonitoringSystem</ApplicationName>
                            </DatabaseConnectionString>
                          </soap:Body>
                        </soap:Envelope>"""

如果要提取URL,请使用以下代码:

import re
url = re.findall("xsi='(.*?)'", body)[0]

如果需要数据库名称: 进口re 数据库名称=关于芬德尔("(.*?)",正文)[0]

这里的关键是(.*?)之外的内容是您想要的东西(例如您的xml标记)左右两侧的字符串,(.*?)本身意味着“为我提取此信息”

只要您知道您正在寻找什么xml标记,就可以提取您正在使用的服务提供给您的任何内容。 函数re.findall返回与您的描述匹配的所有内容的列表。上面的代码假设只有一个东西与您的描述匹配,因此它将只返回列表的第一个元素。你知道吗

相关问题 更多 >