简体   繁体   English

Python minidom无法解析xml

[英]Python minidom can't parse xml

I have xml.file as below, and I'm trying to parse it with python minidom, but there are some problems. 我有如下所示的xml.file,并且我尝试使用python minidom进行解析,但是存在一些问题。 I want to extract some attributes as <ManagedElementId string or <associatedSite string = "Site=site00972"/> but no luck. 我想将某些属性提取为<ManagedElementId string<associatedSite string = "Site=site00972"/>但没有运气。 With python minidom tutorials on internet I didn't manage to do it, so I need you help to show me how to do it. 在互联网上的python minidom教程中,我没有做到这一点,因此我需要您的帮助来向我展示如何做到这一点。 Here is my try: 这是我的尝试:

#!/usr/bin/python 
import os
import xml.dom.minidom
from xml.dom import minidom
from xml.dom.minidom import parseString,parse
from xml.dom.minidom import Node


xmldoc = minidom.parse("proba.xml")

model= xmldoc.getElementsByTagName('ManagedElementId string = ')
for node in model:
    print node.firstChild.nodeValue

and I want to get value between string. 我想获得字符串之间的价值。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE Model SYSTEM "/opt/ericsson/arne/etc/arne12_2.dtd">
<Model version = "1" importVersion = "12.2">
<!--Validate: /opt/ericsson/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>
        </SubNetwork>
    </Create>
</Model>

You are including an attribute name in your tag name: 您在标签名称中包含一个属性名称:

model= xmldoc.getElementsByTagName('ManagedElementId string = ')

The string = part in not part of the tag name; string =部分而不是标签名称的一部分; there are no such tags in your document. 您的文档中没有此类标签。 Drop the string = part: 删除string =部分:

>>> from xml.dom import minidom
>>> tree = minidom.parseString(sample)
>>> tree.getElementsByTagName('ManagedElementId')
[<DOM Element: ManagedElementId at 0x1080baef0>]

This element has no child nodes; 该元素没有子节点。 it only has an attribute value: 它只有一个属性值:

>>> node = tree.getElementsByTagName('ManagedElementId')[0]
>>> node.firstChild is None
True
>>> node.getAttribute('string')
u'siu009722'

I strongly recommend you stay away from the XML DOM, however; 但是,我强烈建议您远离XML DOM。 you'd be far better off using the far simpler ElementTree API here: 您最好在这里使用更简单的ElementTree API

>>> from xml.etree import ElementTree as ET
>>> tree = ET.fromstring(sample)
>>> tree.find('.//ManagedElementId')
<Element 'ManagedElementId' at 0x1080af950>
>>> tree.find('.//ManagedElementId').get('string')
'siu009722'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM