parse a special xml in python

Posted by zhaojing on Stack Overflow See other posts from Stack Overflow or by zhaojing
Published on 2012-11-09T10:38:27Z Indexed on 2012/11/09 11:01 UTC
Read the original article Hit count: 162

Filed under:
|
|
|

I have s special xml file like below:

<alarm-dictionary source="DDD" type="ProxyComponent">

    <alarm code="402" severity="Alarm" name="DDM_Alarm_402">
    <message>Database memory usage low threshold crossed</message>
    <description>dnKinds = database
    type = quality_of_service
    perceived_severity = minor
    probable_cause = thresholdCrossed
    additional_text = Database memory usage low threshold crossed
    </description>
    </alarm>

        ...
</alarm-dictionary>

I know in python, I can get the "alarm code", "severity" in tag alarm by:

for alarm_tag in dom.getElementsByTagName('alarm'):
    if alarm_tag.hasAttribute('code'):
        alarmcode = str(alarm_tag.getAttribute('code'))

And I can get the text in tag message like below:

for messages_tag in dom.getElementsByTagName('message'):
    messages = ""
    for message_tag in messages_tag.childNodes:
        if message_tag.nodeType in (message_tag.TEXT_NODE, message_tag.CDATA_SECTION_NODE):
            messages += message_tag.data

But I also want to get the value like dnkind(database), type(quality_of_service), perceived_severity(thresholdCrossed) and probable_cause(Database memory usage low threshold crossed ) in tag description.

That is, I also want to parse the content in the tag in xml.

Could anyone help me with this? Thanks a lot!

© Stack Overflow or respective owner

Related posts about python

Related posts about Xml