- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个大的 xml 文件(cca 2GB),下面只是该文件的一部分。在此文件中,我想搜索以以下内容开头的所有元素: <ManagedElement sourceType = "SIU">
和<ManagedElement sourceType = "CELLO">
然后从中获取一些数据。下面是这些 ManagedElement 下的内容的示例。这是我的代码:
#!/usr/bin/python
from xml.etree import ElementTree as ElementTree
with open('/var/opt/arne/OSSRC_Model_2014_11_24_3_19_36_PM.xml', 'rt') as f:
tree = ElementTree.parse(f)
with open('/home/gf', 'a') as f1:
for item in tree.findall('.//ManagedElement'):
rank = item.find('.//ipAddress')
rank1=rank.get('string')
name = item.find('.//ManagedElementId')
name1=name.get('string')
site = item.find('.//associatedSite')
site1=site.get('string')
f1.write((name1 + ',' + rank1 + ',' + site1 + '\n'))
问题是还有其他 ManagedElements,而我只想要带有字符串 SIU 和 CELLO 的 ManagedElements。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE Model SYSTEM "/opt/arne/etc/arne12_2.dtd">
<Model version = "1" importVersion = "12.2">
<!--Validate: /opt/arne/bin/import.sh -f 4_siu_create.xml \ -val:rall -->
<Create>
<SubNetwork userLabel = "ZLNOUR_SIU" networkType = "IPRAN">
<ManagedElement sourceType = "SIU">
<ManagedElementId string = "siu009722"/>
<primaryType type = "STN"/>
<managedElementType types = ""/>
<associatedSite string = "Site=site00972"/>
<nodeVersion string = "T11A"/>
<platformVersion string = ""/>
<swVersion string = ""/>
<vendorName string = ""/>
<userDefinedState string = ""/>
<managedServiceAvailability int = "1"/>
<isManaged boolean = "true"/>
<connectionStatus string = "OFF"/>
<Connectivity>
<DEFAULT>
<emUrl url = "http://10.131.203.117:80/"/>
<ipAddress string = "10.131.203.117"/>
<oldIpAddress string = "int dummy=0"/>
<hostname string = ""/>
<nodeSecurityState state = "ON"/>
<boardId string = ""/>
<Protocol number = "0">
<protocolType string = "SNMP"/>
<port int = "161"/>
<protocolVersion string = "v2c"/>
<securityName string = ""/>
<authenticationMethod string = ""/>
<encryptionMethod string = ""/>
<communityString string = "public"/>
<context string = ""/>
<namingUrl string = ""/>
<namingPort int = ""/>
<notificationIRPAgentVersion string = ""/>
<alarmIRPAgentVersion string = ""/>
<notificationIRPNamingContext context = ""/>
<alarmIRPNamingContext context = ""/>
</Protocol>
<Protocol number = "1">
<protocolType string = "SSH"/>
<port int = "22"/>
<protocolVersion string = ""/>
<securityName string = ""/>
<authenticationMethod string = ""/>
<encryptionMethod string = ""/>
<communityString string = ""/>
<context string = ""/>
<namingUrl string = ""/>
<namingPort int = ""/>
<notificationIRPAgentVersion string = ""/>
<alarmIRPAgentVersion string = ""/>
<notificationIRPNamingContext context = ""/>
<alarmIRPNamingContext context = ""/>
</Protocol>
<Browser>
<browser string = ""/>
<browserURL string = ""/>
<bookname string = ""/>
</Browser>
</DEFAULT>
</Connectivity>
<Tss>
<Entry>
<System string = "siu009722"/>
<Type string = "NORMAL"/>
<User string = "admin"/>
<Password string = "siu009722"/>
</Entry>
<Entry>
<System string = "siu009722"/>
<Type string = "SECURE"/>
<User string = "admin"/>
<Password string = "siu009722"/>
</Entry>
</Tss>
<Relationship>
<AssociableNode TO_FDN = "FtpServer=SMRSSLAVE-rtwaned1o,FtpService=swstore-rtwaned1o" AssociationType = "ManagedElement_to_ftpSwStore"/>
<AssociableNode TO_FDN = "FtpServer=SMRSSLAVE-rtwaned1o,FtpService=cmdown-rtwaned1o" AssociationType = "ManagedElement_to_neTransientCmDown"/>
<AssociableNode TO_FDN = "FtpServer=SMRSSLAVE-rtwaned1o,FtpService=cmup-rtwaned1o" AssociationType = "ManagedElement_to_neTransientCmUp"/>
<AssociableNode TO_FDN = "FtpServer=SMRSSLAVE-rtwaned1o,FtpService=pmup-rtwaned1o" AssociationType = "ManagedElement_to_neTransientPm"/>
<AssociableNode TO_FDN = "ManagementNode=ONRM" AssociationType = "MgmtAssociation"/>
<AssociableNode TO_FDN = "SubNetwork=ZLNOUR3,MeContext=rbs009721,ManagedElement=1,NodeBFunction=1" FROM_FDN = "SubNetwork=ZLNOUR_SIU,ManagedElement=siu009722,StnFunction=STN_ManagedFunction" AssociationType = "StnFunction_to_NodeBFunction"/>
</Relationship>
</ManagedElement>
<ManagedElement sourceType="CELLO">
<ManagedElementId string="3GMUP"/>
<primaryType type="RBS"/>
<managedElementType types=""/>
<associatedSite string="Site=Podgorica"/>
<nodeVersion string=""/>
<platformVersion string=""/>
<swVersion string=""/>
<vendorName string="Ericsson"/>
<userDefinedState string=""/>
<managedServiceAvailability int="1"/>
<isManaged boolean="true"/>
<neMIMVersion string="T.1.201"/>
<connectionStatus string="ON"/>
<ManagedFunction>
<functionType string="NodeB"/>
<supportSystemControl boolean="false"/>
</ManagedFunction>
<Connectivity>
<DEFAULT>
<emUrl url="http://10.132.11.226:80/em/index.html"/>
<ipAddress string="10.132.11.226"/>
<oldIpAddress string=""/>
<hostname string=""/>
<nodeSecurityState state="ON"/>
<boardId string=""/>
<Protocol number="0">
<protocolType string="TELNET"/>
<port int="23"/>
<protocolVersion string=""/>
<securityName string=""/>
<authenticationMethod string=""/>
<encryptionMethod string=""/>
<communityString string=""/>
<context string=""/>
<namingUrl string=""/>
<namingPort int=""/>
<notificationIRPAgentVersion string=""/>
<alarmIRPAgentVersion string=""/>
<notificationIRPNamingContext context=""/>
<alarmIRPNamingContext context=""/>
</Protocol>
<Protocol number="1">
<protocolType string="CORBA"/>
<port int="0"/>
<protocolVersion string=""/>
<securityName string=""/>
<authenticationMethod string=""/>
<encryptionMethod string=""/>
<communityString string=""/>
<context string=""/>
<namingUrl string="http://10.132.11.226:80/cello/ior_files/nameroot.ior"/>
<namingPort int="0"/>
<notificationIRPAgentVersion string="3.2"/>
<alarmIRPAgentVersion string="3.2"/>
<notificationIRPNamingContext context="NOTIFICATION_IRP_VERSION_1_1"/>
<alarmIRPNamingContext context="ALARM_IRP_VERSION_1_1"/>
</Protocol>
<Protocol number="2">
<protocolType string="SFTP"/>
<port int="22"/>
<protocolVersion string=""/>
<securityName string=""/>
<authenticationMethod string=""/>
<encryptionMethod string=""/>
<communityString string=""/>
<context string=""/>
<namingUrl string=""/>
<namingPort int=""/>
<notificationIRPAgentVersion string=""/>
<alarmIRPAgentVersion string=""/>
<notificationIRPNamingContext context=""/>
<alarmIRPNamingContext context=""/>
</Protocol>
<Protocol number="3">
<protocolType string="FTP"/>
<port int="21"/>
<protocolVersion string=""/>
<securityName string=""/>
<authenticationMethod string=""/>
<encryptionMethod string=""/>
<communityString string=""/>
<context string=""/>
<namingUrl string=""/>
<namingPort int=""/>
<notificationIRPAgentVersion string=""/>
<alarmIRPAgentVersion string=""/>
<notificationIRPNamingContext context=""/>
<alarmIRPNamingContext context=""/>
</Protocol>
<Protocol number="4">
<protocolType string="SSH"/>
<port int="22"/>
<protocolVersion string=""/>
<securityName string=""/>
<authenticationMethod string=""/>
<encryptionMethod string=""/>
<communityString string=""/>
<context string=""/>
<namingUrl string=""/>
<namingPort int=""/>
<notificationIRPAgentVersion string=""/>
<alarmIRPAgentVersion string=""/>
<notificationIRPNamingContext context=""/>
<alarmIRPNamingContext context=""/>
</Protocol>
<Browser>
<browser string=""/>
<browserURL string=""/>
<bookname string=""/>
</Browser>
</DEFAULT>
</Connectivity>
<Tss>
<Entry>
<System string="3GMUP"/>
<Type string="SECURE"/>
<User string="rbs"/>
<Password string="rbs"/>
</Entry>
<Entry>
<System string="3GMUP"/>
<Type string="NORMAL"/>
<User string="rbs"/>
<Password string="rbs"/>
</Entry>
</Tss>
<Relationship>
<AssociableNode TO_FDN="FtpServer=SMRSMASTERWRAN-oss1,FtpService=smo-backup-oss1" AssociationType="ManagedElement_to_ftpBackupStore"/>
<AssociableNode TO_FDN="FtpServer=SMRSMASTERWRAN-oss1,FtpService=smo-keystore-oss1" AssociationType="ManagedElement_to_ftpLicenseKeyStore"/>
<AssociableNode TO_FDN="FtpServer=SMRSMASTERWRAN-oss1,FtpService=smo-swstore-oss1" AssociationType="ManagedElement_to_ftpSwStore"/>
<AssociableNode TO_FDN="Group=CD34082729" AssociationType="Group_to_MeContext"/>
<AssociableNode TO_FDN="ManagementNode=ONRM" AssociationType="MgmtAssociation"/>
<AssociableNode TO_FDN="SubNetwork=IPRAN,ManagedElement=MUP_BD,StnFunction=STN_ManagedFunction" FROM_FDN="SubNetwork=RNC111,MeContext=3GMUP,ManagedElement=1,NodeBFunction=1" AssociationType="StnFunction_to_NodeBFunction"/>
<AssociableNode TO_FDN="SubNetwork=RNC111,Group=NodeB" AssociationType="Group_to_MeContext"/>
</Relationship>
</ManagedElement>
</SubNetwork>
</Create>
</Model>
最佳答案
仅获取具有 sourceType
属性的 ManagedElement
的正确 XPath 等于 "CELLO"
或等于 "SIU"
如下:
.//ManagedElement[@sourceType="CELLO" or @sourceType="SIU"]
不幸的是,XPath OR
运算符似乎 not supported by xml.etree.ElementTree
,因此简单地将上面的 XPath 传递给 findall()
方法是行不通的。现在,您可能的选择是使用 lxml具有更好 XPath 支持的库(特别是如果您计划在代码中更广泛地使用复杂的 XPath),或者您可以创建 lambda 表达式以通过其 sourceType
属性手动过滤 ManagedElement
值(value)。例如,不仅仅是:
tree.findall('.//ManagedElement')
...你可以尝试这样的事情:
filter(lambda x: x.get('sourceType') in ['CELLO','SIU'], tree.findall('.//ManagedElement'))
关于python ElementTree 查找带有字符串的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28717738/
我在 GlassFish (J2EE_1.4) 上的 NetBeans 中开发企业项目。我的项目中有一些实体 bean、一些 session bean 和消息驱动 bean。我以如下方式使用 serv
什么在速度方面更好...... 我正在尝试确定用户是否已将某个 URL 添加到他们的快捷方式列表中。如果他们添加了 URL,页面上就会有一个链接,用于从快捷方式中删除该页面,否则他们可以将其添加到快捷
我的问题如下: 我打开一个Excel-File,但我不知道我的客户在模板文件中使用了哪些可能的标头变量。它们可以是:#DATE,#TIME,#NAME等。因此,我需要查找这些变量,以及是否已使用过:替
我有一堆以“-e”结尾的文件要删除。 $ find . -name "*-e" exec rm {} \; find: exec: unknown primary or operator 正则表达式是
我有一个简单的问题:是否可以在 TypeScript 中获取联合的一部分的类型? 例如,您可以经常使用如下查找类型: interface Person { name: string; } type
我正在尝试设置 Point Cloud Library启用 CUDA 选项的主干构建。 我相信我已经按照 these instructions 正确安装了 CUDA . 在 PCL 构建的 cmake
我将首先说我所知道的唯一 VBA 是操作录制的宏的反复试验。我是一名注册会计师,试图以艰难的方式学习 VBA(并希望我去学校学习计算机编程!)。 我有带有多个工作表的大型工作簿。 G 列中以黄色突出显
当文件数达到阈值时,我试图删除目录中最旧的文件。 list_of_files = os.listdir('log') if len([name for name in list_of_files
我有一个数组,它有一些重复的值。 我必须计算每个重复项的数量及其索引。 打印如: Index of b: 1 Index of b: 4 Index of c: 2 Index of c: 3 Ind
我已经搜索了我的问题的解决方案,但没有成功。热键 ctrl+F 找到的 eclipse 查找/替换功能不起作用。注意:通过 Eclipse 菜单 Edit>Find Replace(不工作我的意思是
我想检查 div 是否包含类为“error”的子级,但条件是错误类显示不等于无。 (意味着错误类必须可见。 如何更改我的以下代码: $(".related_field").each(function
这个问题已经有答案了: 已关闭13 年前。 Possible Duplicate: Can jQuery provide the tag name? 嗨! 这个问题太基础了,我不好意思问,但我尝试了
我一直听说这是 cygwin 的路径问题。它阻止了 emacs 在我的 cygwin 中工作。当我在 cli(不是 bash/cygwin)上执行 find 时,无论我输入什么,我都会得到同样的错误。
我正在使用此变量来获取一个或多个与我需要的值相匹配的值。 var mail = $("#dat").contents().find("td:contains('" + name + "')" ).si
请原谅这个长问题。我只是不确定解决这个问题的最佳方法是什么。 我有一个电子表格(Google 表格),其中包含用户和地址列表,我需要从中创建邮寄标签。该电子表格是从我们的学生信息系统导出的。这些地址应
我正在 Excel VBA 中创建一个公式,以解析单元格中以逗号分隔的“部分”列表。在另一个工作表中查找具有该零件名称的单元格,然后使用找到的该单元格的地址来获取同一行不同列的零件成本。我为此工作了数
我被要求在网络应用程序上实现一些电子邮件地址验证 - 我确信我们都已经经历过一千次了...但是,这一次我被要求在域上进行 MX 查找查看它是否接受电子邮件。 有人知道这样做有任何潜在的问题吗? mx
我有一个切换按钮,可读取.wave文件,并且字符串更改为暂停,然后..... 我的问题是,当用户播放声音时,按钮字符串更改为暂停,结束声音后,该字符串仍为暂停状态,我想将其更改为播放。但是我不知道如何
对于令人困惑的标题提前表示歉意。我的问题如下,我在大约 600 个文件中有以下文本: $_REQUEST['FOO'] 我想将其替换为以下内容: $this->input->post('FOO') 为
我正在使用 Ansible 的查找功能查找 INI 文件中的值。这是文档中的示例: - debug: msg="User in integration is {{ lookup('ini', 'use
我是一名优秀的程序员,十分优秀!