试图用python中的元素树对与标签关联的特定值分析XML行

发布于 2025-02-13 08:51:45 字数 937 浏览 2 评论 0原文

我目前正在努力试图弄清楚如何从XML文件中的特定标签中解析特定信息。

XML文件的结构是这样,因此

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <configSections>
        |
        |
   </configSections>
   <connectionStrings>
        <clear />
        <add name="Database1" connectionString="DataSource=[ServerName];Initial Catalog=[DatabaseName]; User ID=user2;Password=oogabooga1" providerName="Database1provider"/>
        <add name="Database12" connectionString="DataSource=[ServerName];Initial Catalog=[DatabaseName]; User ID=user1;Password=oogabooga2" providerName="Database2provider"/>

   </connectionStrings>

我正在尝试从Connectionsstrings中解析名称,用户ID和密码。

我坚持尝试使用元素树来获取数据,这里有什么想法吗?

import xml.etree.ElementTree as ET

webconfig = etree.parse(filepath)

from elem in webconfig.findall('configuration'):
    print(elem.text)

任何帮助都非常感谢

I am currently struggling trying to figure out how to parse out specific information from a specific tag that is in a xml file.

The xml file is structured as such

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <configSections>
        |
        |
   </configSections>
   <connectionStrings>
        <clear />
        <add name="Database1" connectionString="DataSource=[ServerName];Initial Catalog=[DatabaseName]; User ID=user2;Password=oogabooga1" providerName="Database1provider"/>
        <add name="Database12" connectionString="DataSource=[ServerName];Initial Catalog=[DatabaseName]; User ID=user1;Password=oogabooga2" providerName="Database2provider"/>

   </connectionStrings>

I am trying to parse out the name, UserId and Password from connectionStrings.

I am stuck on trying to get parse out the data using Element Tree, any ideas here?

import xml.etree.ElementTree as ET

webconfig = etree.parse(filepath)

from elem in webconfig.findall('configuration'):
    print(elem.text)

Any help is greatly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那伤。 2025-02-20 08:51:45

这有帮助吗?


import xml.etree.ElementTree as ET

someXML = '''<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <configSections>
        |
        |
   </configSections>
   <connectionStrings>
        <clear />
        <add name="Database1" connectionString="DataSource=[ServerName];Initial Catalog=[DatabaseName]; User ID=user2;Password=oogabooga1" providerName="Database1provider"/>
        <add name="Database12" connectionString="DataSource=[ServerName];Initial Catalog=[DatabaseName]; User ID=user1;Password=oogabooga2" providerName="Database2provider"/>
   </connectionStrings>
</configuration>
'''

data = ET.fromstring(someXML)

for i in data.findall('connectionStrings'):
    for item in i.findall('add'):
        print(item.attrib)

请注意,XML中有几个语法问题。

另外,如果您需要获得单个密钥值对,则可以使用以下方式:


for i in data.findall('connectionStrings'):
    for item in i.findall('add'):
        print(item.get('name'))

Does this help?


import xml.etree.ElementTree as ET

someXML = '''<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <configSections>
        |
        |
   </configSections>
   <connectionStrings>
        <clear />
        <add name="Database1" connectionString="DataSource=[ServerName];Initial Catalog=[DatabaseName]; User ID=user2;Password=oogabooga1" providerName="Database1provider"/>
        <add name="Database12" connectionString="DataSource=[ServerName];Initial Catalog=[DatabaseName]; User ID=user1;Password=oogabooga2" providerName="Database2provider"/>
   </connectionStrings>
</configuration>
'''

data = ET.fromstring(someXML)

for i in data.findall('connectionStrings'):
    for item in i.findall('add'):
        print(item.attrib)

Please note there were several syntax issues in your XML which I cleaned up.

Also, if you need to get individual key value pairs, you can use this:


for i in data.findall('connectionStrings'):
    for item in i.findall('add'):
        print(item.get('name'))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文