0

Xml which need to be parsed "cos1.XML"

<config xmlns="http://tail-f.com/ns/config/1.0">
  <sys xmlns="urn:XYZ:ns:yang:app:4.3.3.0">
  <app>
  <Feature>
    <name>0</name>
    <FeatureID>default</FeatureID>
    <param>MaxVoiceMessageLength</param>
    <value>120s</value>
  </Feature>
  <Feature>
    <name>96</name>
    <FeatureID>default</FeatureID>
    <param>MCNType</param>
    <value>CLIAggregation</value>
  </Feature>
  <Feature>
    <name>97</name>
    <FeatureID>default</FeatureID>
    <param>SM_HOUR_FORMAT</param>
    <value>24_HR</value>
  </Feature>
  <Feature>
    <name>99</name>
    <FeatureID>default</FeatureID>
    <param>MCNRecordsOrder</param>
    <value>LIFO</value>
  </Feature>
  </app>
  </sys>
</config>

This is Python script I am using to Parse the XMl to get "param" and "value" tag.But findall is return empty.

import xml.etree.ElementTree as ET
import sys
def modifycos():

    tree = ET.parse(cos1.xml)
    root = tree.getroot()
    for cos in root.findall('./config/sys/app/Feature')
        parameter = cos.find('param').text
        parmvalue = cos.get('value')
        print(parameter, parmvalue)

modifycos()

(MaxVoiceMessageLength, '120s') (MCNType, 'CLIAggregation') (SM_HOUR_FORMAT, '24_HR') (MCNRecordsOrder,'LIFO')

2 Answers 2

1

Here are a few things that you can do to make sure that you're hitting up the right file-

I don't see the name of the .XML file mentioned in this following line -

for cos in root.findall('./config/sys/app/Feature'):

Make sure to enter the name of your file in this code like this-

for cos in root.findall('./config/sys/app/Feature/cos1.XML'):

If it still does not work, try to define a proper path to the file-

import os
current_path = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.join(current_path+'/config/sys/app/Feature/cos1.XML')

This should work. Let me know if it helps. :)

Sign up to request clarification or add additional context in comments.

2 Comments

It is parsing the xml file properly and got the root and tree as well but below line is creating problem for me. <br/> for cos in root.findall('./config/sys/app/Feature'). In this line root.findall() is not return anything.
Try replacing that line with for root, dirs, files in os.walk('./config/sys/app/Feature'): and see if it works. You will get all the files in the files variable so you might want to loop over it again.
0

Try this:

import xml.etree.ElementTree as ET import sys

def modifycos():

    tree = ET.parse("try.xml")

    root = tree.getroot()

    sys = root.getchildren()[0]
    app = sys.getchildren()[0]
    features = app.getchildren()
    for element in features:
        childs = element.getchildren()
        for child in childs:
            if "param" in child.tag:
                parameter = child.text

            if "value" in child.tag:
                paramvalue = child.text
        print(parameter , paramvalue)

This will give you desired results.

2 Comments

This is not working as root.findall() is not returning any value to cos. So it is not going inside the for loop.
the problem is with the xml structure , the parser is not recognising tags

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.