0

How can I read a xml file and load the content, similar to the json example. json code shared is for reference which is working and I want to load xml content in a similar way. Have tried minidom parse in xml but when I tried to get the methods from readed content, it throws me error AttributeError: keys_exists() expects dict as first argument.

with open(xml_file) as f:
        content = f.read()
        self.spec = xml.loads(content)

json file can load the content as : 
  with open(json_file) as f:
    content = f.read()
    self.spec = json.loads(content)

Have tried minidom parse for xml :

    with open(xml_file) as f:
       self.spec = minidom.parse(xml_file)

Trying to get the methods but it displayed the error AttributeError: keys_exists() expects dict as first argument. on execution:

 def get_methods(self):
      ''' Get all the methods 
      Return:
      All the methods (array)          
      '''
      list_methods = []
      if keys_exists(self.spec,self.name,'methods'):
             for action in self.spec[self.name]['methods'].keys():
                   for method in self.spec[self.name]['methods'] [action].keys():
                        if method == 'method':

                                list_methods.append(self.spec[self.name]['method'] [action][method])
      return list_methods

xml file format:

<class>
<grpname>test123</grpname>
<name>test123_binding</name>
<descr>test</descr>
<properties>
    <property>
        <name>test</name>
        <type>test_binding[]</type>
        <access>readonly</access>
    </property>
    <property>
        <name>test_action_binding</name>
        <type>test_action_binding[]</type>
        <access>readonly</access>
    </property>
</properties>
<methods>
    <method>
        <name>get</name>
        <in-parameters>
            <parameter>
                <name>session</name>
                <type>service</type>
                <mandatory>true</mandatory>
            </parameter>
        </in-parameters>
        <out-parameter>
            <parameter>
                <type>test_binding</type>
            </parameter>
        </out-parameter>
    </method>
</methods>

Pooja
  • 481
  • 1
  • 8
  • 15
  • 1
    In Python when working with XML I often use the lxml library https://lxml.de/ or a derivative of that work. Commonly a little harder than JSON. – Marc Apr 23 '19 at 19:26
  • Possible duplicate of [Python XML File Open](https://stackoverflow.com/questions/18834393/python-xml-file-open) – Avi Thour Apr 23 '19 at 19:27
  • @AviThour Have tried using minidom.parse(xml_file) but got the error while I tried to get the methods. – Pooja Apr 24 '19 at 04:04
  • 1
    @Pooja - How should the dict that holds the method look like? Please share an example. Another point is that the XML you have posted is not a valid XML. Please make it a valid XML. – balderman Apr 24 '19 at 09:01

0 Answers0