LXML树连接和属性
我有一个.dtsx
文件,因此,我有多个具有连接的组件,因此我需要提取具有特殊连接的组件,但是我无法处理,例如:
<components>
<component refId="Component_1 Name" componentClassID="componentClassID" contactInfo="contactInfo" description="description" name="name" usesDispositions="true" version="6">
<properties>
<property dataType="System.String" description="description" expressionType="Notify" name="SqlCommandParam" UITypeEditor="UITypeEditor">QUERY THAT i NEED TO GET</property>
</properties>
<connections>
<connection refId="Name" connectionManagerID="Package.ConnectionManagers[BI_SYNC]" connectionManagerRefId="Package.ConnectionManagers[BI_SYNC]" description="description" name="OleDbConnection" />
</connections>
</component>
<component refId="Component_2 Name" componentClassID="componentClassID" contactInfo="contactInfo" description="description" name="PartnerService" usesDispositions="true" version="6">
<properties>
<property dataType="System.String" description="description" expressionType="Notify" name="SqlCommandParam" UITypeEditor="UITypeEditor">QUERY THAT I DONT NEED TO GET</property>
</properties>
<connections>
<connection refId="Name" connectionManagerID="Package.ConnectionManagers[BI_STG]" connectionManagerRefId="Package.ConnectionManagers[BI_STG]" description="description" name="OleDbConnection" />
</connections>
</component>
</components>
我需要获取查询,其中< 相同级别,属性和连接代码为+/- like:
代码> ConnectionManagerId =“ package.connectionManagers [bi_sync]” ,但我无法处理,因为它们处于我使用的
for cnt, element in enumerate(root.xpath(".//*")):
if cnt == 0:
file = root.attrib["{www.microsoft.com/SqlServer/Dts}ObjectName"]
data["file_name"] = file + ".dtsx"
if element.tag == con_tag:
if element.attrib.get("{www.microsoft.com/SqlServer/Dts}ObjectName"):
if element.attrib.get("{www.microsoft.com/SqlServer/Dts}ObjectName", None) == "BI_SYNC":
conn_name = element.attrib.get("{www.microsoft.com/SqlServer/Dts}ObjectName", None)
conn_dtsid = element.attrib.get("{www.microsoft.com/SqlServer/Dts}DTSID", None)
data["conn_name"] = conn_name
data["conn_dtsid"] = conn_dtsid
if element.tag == exec_tag:
for cnt_0, element_0 in enumerate(element):
if element_0.tag == execs_tag:
for cnt_1, element_1 in enumerate(element_0): # Get package name
I have a .dtsx
file so, I have multiple components with connections, so I need to extract component that have especific connection, but I can not handle that, example:
<components>
<component refId="Component_1 Name" componentClassID="componentClassID" contactInfo="contactInfo" description="description" name="name" usesDispositions="true" version="6">
<properties>
<property dataType="System.String" description="description" expressionType="Notify" name="SqlCommandParam" UITypeEditor="UITypeEditor">QUERY THAT i NEED TO GET</property>
</properties>
<connections>
<connection refId="Name" connectionManagerID="Package.ConnectionManagers[BI_SYNC]" connectionManagerRefId="Package.ConnectionManagers[BI_SYNC]" description="description" name="OleDbConnection" />
</connections>
</component>
<component refId="Component_2 Name" componentClassID="componentClassID" contactInfo="contactInfo" description="description" name="PartnerService" usesDispositions="true" version="6">
<properties>
<property dataType="System.String" description="description" expressionType="Notify" name="SqlCommandParam" UITypeEditor="UITypeEditor">QUERY THAT I DONT NEED TO GET</property>
</properties>
<connections>
<connection refId="Name" connectionManagerID="Package.ConnectionManagers[BI_STG]" connectionManagerRefId="Package.ConnectionManagers[BI_STG]" description="description" name="OleDbConnection" />
</connections>
</component>
</components>
I need to get the query where connectionManagerID="Package.ConnectionManagers[BI_SYNC]"
But I cannot handle that because they are in the same level, properties and connections
Code that I am using is +/- like:
for cnt, element in enumerate(root.xpath(".//*")):
if cnt == 0:
file = root.attrib["{www.microsoft.com/SqlServer/Dts}ObjectName"]
data["file_name"] = file + ".dtsx"
if element.tag == con_tag:
if element.attrib.get("{www.microsoft.com/SqlServer/Dts}ObjectName"):
if element.attrib.get("{www.microsoft.com/SqlServer/Dts}ObjectName", None) == "BI_SYNC":
conn_name = element.attrib.get("{www.microsoft.com/SqlServer/Dts}ObjectName", None)
conn_dtsid = element.attrib.get("{www.microsoft.com/SqlServer/Dts}DTSID", None)
data["conn_name"] = conn_name
data["conn_dtsid"] = conn_dtsid
if element.tag == exec_tag:
for cnt_0, element_0 in enumerate(element):
if element_0.tag == execs_tag:
for cnt_1, element_1 in enumerate(element_0): # Get package name
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
属性
和连接
都是相同component
的孩子,因此您可以使用XPath选择基于component
基于连接
,然后选择属性
。因此,请尝试使用类似的内容,而不是在陈述中嵌套很多,
这会输出以下内容,以表明它选择了正确的
属性
...有几个注释...
root
带有默认命名空间的元素,因此我的root
变量将与您已经拥有的变量相似。命名空间
kwarg,因此我可以在XPATH中使用前缀,而不是使用Clark符号。 (我认为更清洁。)Since
properties
andconnections
are both children of the samecomponent
, you can use xpath to select thecomponent
based on theconnection
, then select theproperty
.So instead of a lot of nested if and for statements, try something like...
This outputs the following to show that it selects the correct
property
...A couple of notes...
root
element with the default namespace so myroot
variable would work similar to what you already have.namespaces
kwarg so I could use a prefix in my xpath instead of using Clark notation. (Cleaner in my opinion.)