2

I was wondering if there is a way to parse through the XML below and get most of the tags, including the nested ones and put them into columns and rows without hardcoding.

<?xml version="1.0" encoding="UTF-8"?>

<faults version="1" xmlns="urn:nortel:namespaces:mcp:faults" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:nortel:namespaces:mcp:faults NortelFaultSchema.xsd ">
    <family longName="1OffMsgr" shortName="OOM"/>
    <family longName="ACTAGENT" shortName="ACAT">
        <logs>
           <log>
                <eventType>RES</eventType>
                <number>1</number>
                <severity>INFO</severity>
                <descTemplate>
                     <msg>Accounting is enabled upon this NE.</msg>
               </descTemplate>
               <note>This log is generated when setting a Session Manager's AM from &lt;none&gt; to a valid AM.</note>
               <om>On all instances of this Session Manager, the &lt;NE_Inst&gt;:&lt;AM&gt;:STD:acct OM row in the  StdRecordStream group will appear and start counting the recording units sent to the configured AM.
                   On the configured AM, the &lt;NE_inst&gt;:acct OM rows in RECSTRMCOLL group will appear and start counting the recording units received from this Session Manager's instances.
               </om>
            </log>
           <log>
                <eventType>RES</eventType>
                <number>2</number>
                <severity>ALERT</severity>
                <descTemplate>
                     <msg>Accounting is disabled upon this NE.</msg>
               </descTemplate>
               <note>This log is generated when setting a Session Manager's AM from a valid AM to &lt;none&gt;.</note>
               <action>If you do not intend for the Session Manager to produce accounting records, then no action is required.  If you do intend for the Session Manager to produce accounting records, then you should set the Session Manager's AM to a valid AM.</action>
               <om>On all instances of this Session Manager, the &lt;NE_Inst&gt;:&lt;AM&gt;:STD:acct OM row in the StdRecordStream group that matched the previous datafilled AM will disappear.
                   On the previously configured AM, the  &lt;NE_inst&gt;:acct OM rows in RECSTRMCOLL group will disappear.
               </om>
            </log>
        </logs>
    </family>
    <family longName="ACODE" shortName="AC">
        <alarms>
            <alarm>
                <eventType>ADMIN</eventType>
                <number>1</number>
                <probableCause>INFORMATION_MODIFICATION_DETECTED</probableCause>
                <descTemplate>
                    <msg>Configured data for audiocode server updated: $1</msg>
                     <param>
                         <num>1</num>
                         <description>AudioCode configuration data got updated</description>
                         <exampleValue>acgwy1</exampleValue>
                     </param>
               </descTemplate>
               <manualClearable></manualClearable>
               <correctiveAction>None. Acknowledge/Clear alarm and deploy the audiocode server if appropriate.</correctiveAction>
               <alarmName>Audiocode Server Updated</alarmName>
               <severities>
                     <severity>MINOR</severity>
               </severities>               
            </alarm>
            <alarm>
                <eventType>ADMIN</eventType>
                <number>2</number>
                <probableCause>CONFIG_OR_CUSTOMIZATION_ERROR</probableCause>
                <descTemplate>
                    <msg>Deployment for audiocode server failed: $1. Reason: $2.</msg>
                     <param>
                         <num>1</num>
                         <description>AudioCode Name</description>
                         <exampleValue>audcod</exampleValue>
                     </param>
                     <param>
                         <num>2</num>
                         <description>AudioCode Deployment failed reason</description>
                         <exampleValue>Failed to parse audiocode configuration data</exampleValue>
                     </param>
               </descTemplate>
               <manualClearable></manualClearable>
               <correctiveAction>Check the configuration of audiocode server. Acknowledge/Clear alarm and deploy the audiocode server if appropriate.</correctiveAction>
               <alarmName>Audiocode Server Deploy Failed</alarmName>
               <severities>
                     <severity>MINOR</severity> 
                     <severity>MAJOR</severity>
               </severities>               
            </alarm>
            <alarm>
                <eventType>COMM</eventType>
                <number>2</number>
                <probableCause>LOSS_OF_FRAME</probableCause>
                <descTemplate>
                    <msg>Far end LOF (a.k.a., Yellow Alarm). Trunk (DS1 Number): $1.</msg>
                     <param>
                         <num>1</num>
                         <description>Trunk Number of Trunk with configuration problem</description>
                         <exampleValue>2</exampleValue>
                     </param>
               </descTemplate>
               <clearCondition>Far end is correctly configured for proper framing.</clearCondition>
               <correctiveAction>Check that the far end is configured for the proper framing.</correctiveAction>
               <alarmName>Far end LOF</alarmName>
               <severities>
                     <severity>CRITICAL</severity>
               </severities>
               <note>This alarm indicates the Trunk Framing settings on the connected PSTN switch do not match those provisioned on the Audiocodes Mediant 2k.</note>
            </alarm>
            <alarm>
                <eventType>COMM</eventType>
                <number>3</number>
                <probableCause>LOSS_OF_FRAME</probableCause>
                <descTemplate>
                    <msg>Near end sending LOF Indication. Trunk (DS1 Number): $1.</msg>
                     <param>
                         <num>1</num>
                         <description>Trunk Number of Trunk with configuration problem</description>
                         <exampleValue>2</exampleValue>
                     </param>
               </descTemplate>
               <clearCondition>Gateway is correctly configured for proper framing.</clearCondition>
               <correctiveAction>Check that the Audiocodes gateway is configured for the proper framing.</correctiveAction>
               <alarmName>Near end sending LOF Indication</alarmName>
               <severities>
                     <severity>CRITICAL</severity>
               </severities>               
            </alarm>
        </alarms>
        <logs>
           <log>
                <eventType>ABNORMAL</eventType>
                <number>1</number>
                <severity>ALERT</severity>
                <descTemplate>
                     <msg>Failed to deploy audiocode server. Server Name: $1, Failed At: $2</msg>
                     <param>
                         <num>1</num>
                         <description>IP address of gateway which failed.</description>
                         <exampleValue>192.168.0.1</exampleValue>
                     </param>
                     <param>
                         <num>2</num>
                         <description>One of the following: "Parse Configuration Data","Upload Tone File","Upload Load File" and "Upload Configuration File"</description>
                         <exampleValue>Parse Configuration Data</exampleValue>
                     </param>
               </descTemplate>
               <note>There was a problem during the commissioning/upgrade of a gateway.  Either the configuration file was corrupt or files could not be uploaded to the gateway.</note>
               <action>Examine the MCS logs as well as the syslogs from the gateway to determine what is causing the problem.</action>
            </log>
           <log>
                <eventType>ABNORMAL</eventType>
                <number>2</number>
                <severity>ALERT</severity>
                <descTemplate>
                     <msg>Failed to restart audiocode server. Server Name: $1. Exception caught: $2</msg>
                     <param>
                         <num>1</num>
                         <description>Server Long Name</description>
                         <exampleValue>audiocode_gateway_1</exampleValue>
                     </param>
                     <param>
                         <num>2</num>
                         <description>Exception occured during restarting the server.</description>
                         <exampleValue>[example Java exception traceback not given]</exampleValue>
                     </param>
               </descTemplate>
               <note>The AudioCodes Gateway was unable to be restarted due to a problem found in the INI file.</note>
               <action>Examine the configuration file and the syslogs of the gateway to determine what the configuration error is.  Correct this, then restart the server.</action>
            </log>
     </logs>
    </family>
</faults>

The code is basically doing this but it is not getting the nested elements inside the descTemplate tag. I would like to find an effective solution to parse through all elements including the nested ones without hard-coding (or as little as possible).

To further elaborate on what the program does: For example if we look at the eventType tag in my xml. It creates a column named "eventType" and put the value inside it underneath that column. Each "eventType" tag it parses through would be put it into the same column.

In a previous very very similar question tdelaney generously provided this code which I haven’t figured out how to extend to solve my problem so I thought I’d ask again - thank you tdelaney:

import csv
import lxml.etree
from lxml.etree import QName
import operator

class ExpandingTable:
    """A 2 dimensional table where columns are exapanded as new column
    types are discovered"""

    def __init__(self):
        """Create table that can expand rows and columns"""
        self.name_to_col = {}
        self.table = []
    
    def add_column(self, name):
        """Add column named `name` unless already included"""
        if name not in self.name_to_col:
            self.name_to_col[name] = len(self.name_to_col)
            for row in self.table:
                row.append('')
    
    def add_cell(self, name, value):
        """Add value to named column in the current row"""
        if value:
            self.add_column(name)
            self.table[-1][self.name_to_col[name]] = value.strip().replace("\r\n", " ")
            
    def new_row(self):
        """Create a new row and make it current"""
        self.table.append([''] * len(self.name_to_col))

    def header(self):
        """Gather discovered column names into a header list"""
        idx_1 = operator.itemgetter(1)
        return [name for name, _ in sorted(self.name_to_col.items(), key=idx_1)]

    def prepend_header(self):
        """Gather discovered column names into a header and
        prepend it to the list"""
        self.table.insert(0, self.header())

def events_to_table(elem):
    """ Builds table from <family> child elements and their contained alarms and
    logs."""
    ns = {"f":"urn:nortel:namespaces:mcp:faults"}
    table = ExpandingTable()
    for family in elem.xpath("f:family", namespaces=ns):
        longName = family.get("longName")
        shortName = family.get("shortName")
        for event in family.xpath("*/*[f:eventType]", namespaces=ns):
            table.new_row()
            table.add_cell("longName", longName)
            table.add_cell("shortName", shortName)
            for cell in event:
                tag = QName(cell.tag).localname
                if tag == "severities":
                    tag = "severity"
                    text = ",".join(severity.text for severity in cell.xpath("*"))
                    print("severities", repr(text))
                else:
                    text = cell.text
                table.add_cell(tag, text)
    table.prepend_header()
    return table.table
    
def main(filename):
    doc = lxml.etree.parse(filename)
    table = events_to_table(doc.getroot())
    with open('test.csv', 'w', newline='', encoding='utf-8') as fileobj:
        csv.writer(fileobj).writerows(table)

main('OMGroups.xml')

Any help would be greatly appreciated.

0

1 Answer 1

3

Try this.

from simplified_scrapy import SimplifiedDoc, utils


def getKeyValues(nodeCols, dic, header):
    for nodeCol in nodeCols:
        childCols = nodeCol.children
        if childCols:
            getKeyValues(childCols, dic, header)
        else:
            tag = nodeCol.tag
            v = dic.get(tag)
            if v:  # Cases with multiple values
                dic[tag] = v + '|' + nodeCol.text # Splicing into 1 column
                # i = 1
                # while True:
                #     tag = tag + str(i)
                #     v = dic.get(tag)
                #     if v == None:
                #         dic[tag] = nodeCol.text
                #         break
                #     i = i + 1
            else:
                dic[tag] = nodeCol.text

            if tag not in header:
                header.append(tag)


xml = utils.getFileContent('OMGroups.xml')
doc = SimplifiedDoc(xml)  # create doc
header = ['longName','shortName','nodeType'] # add column
dicRow = []
# nodes = doc.faults.children.child
parentNodes = doc.faults.children.children # add
for nodes in parentNodes: # add
   for node in nodes:  # logs,alarms...
      if not node:
         continue
      family = node.parent
      longName = family['longName'] # get the value
      shortName = family['shortName']
      nodeRows = node.children
      for nodeRow in nodeRows:  # log,log...
         dicCol = {'longName': longName, 'shortName': shortName, 'nodeType': nodeRow.tag}
         nodeCols = nodeRow.children  # eventType,number
         getKeyValues(nodeCols, dicCol, header)
         dicRow.append(dicCol)

# Prepare the data and store it in the csv file
rows = [header]
for dic in dicRow:
    rows.append([dic.get(k) for k in header])

utils.save2csv('test.csv', rows, newline='')
Sign up to request clarification or add additional context in comments.

13 Comments

Thank you very much for this, but I was thinking, do you think it would be possible to just put everything inside each descTemplate into 1 cell? So like all tags msg, num description, and example value all squished into 1 cell for each alarm/log? If I could get the original code in my question to do that, then that would be perfect.
@marcorivera8 I changed the answer, you see if it's what you want.
Hey @Yazz, it's pretty close thank you! But how can I get into family tag and get the longName and shortName and put them into the first 2 columns?
@marcorivera8 dicCol = {'longName': longName + nodeRow.select('eventType>text()') ...
@marcorivera8 It doesn't matter. I updated the answer.
|

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.