2

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!

3 Answers 3

4

Once you have the text from the description tag, it's nothing to do with XML parsing. You just need do simple string-parsing to get the type = quality_of_service keys/values strings into something nicer to use in Python like a dictionary

With some slightly simpler parsing thanks to ElementTree, it would look like this

messages = """
<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>
"""

import xml.etree.ElementTree as ET

# Parse XML
tree = ET.fromstring(messages)

for alarm in tree.getchildren():
    # Get code and severity
    print alarm.get("code")
    print alarm.get("severity")

    # Grab description text
    descr = alarm.find("description").text

    # Parse "thing=other" into dict like {'thing': 'other'}
    info = {}
    for dl in descr.splitlines():
        if len(dl.strip()) > 0:
            key, _, value = dl.partition("=")
            info[key.strip()] = value.strip()
    print info
Sign up to request clarification or add additional context in comments.

Comments

2

I'm not completely sure on Python, but after quick research.

Seeing as you can already get all of the content from the description tag in XML, can you not split by line breaks, and then split each line using the str.split() function on the equals signs to give you name / value separately?

e.g.

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
tag =  str.split('=');
tagName = tag[0]
tagValue = tag[1]

(I haven't taken into account splitting each line up and looping)

But that should get you on the right track :)

4 Comments

Not valid Python, but that's definitely the way to solve the problem - unless the description was something like <info key="dnKinds">database</info>, it's not an XML problem
Hehe, I had a feeling it wasn't but Python isn't my speciality (Haven't used it in a few years) so I just quickly perused the Python docs to provide an answer with some sort of good logic at least.
@ldiot211, Thanks a lot! yes, I think your solution definitely solve the problem!
Glad to have been able to help sir!
2

AFAIK there is no library to handle the text as DOM elements.

You can however (after you have the message in the message variable) do:

description = {}
messageParts = message.split("\n")
for part in messageParts:
    descInfo = part.split("=")
    description[descInfo[0].strip()] = descInfo[1].strip()

then you'll have inside description the information you need in the form of a key-value map.

You should also add error handling on my code...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.