试图用python中的元素树对与标签关联的特定值分析XML行
我目前正在努力试图弄清楚如何从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有帮助吗?
请注意,XML中有几个语法问题。
另外,如果您需要获得单个密钥值对,则可以使用以下方式:
Does this help?
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: