Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have parsed XML file looking like this:

 <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE raml SYSTEM 'raml20.dtd'>
    <raml version="2.0" xmlns="raml20.xsd">
    <cmData type="actual">
        <managedObject class="LN" distName="PTR" id="2425">
          <p name="aak">220</p>
          <p name="orp">05</p>
          <p name="name">Portro</p>
          <p name="optres">false</p>
          <p name="optblu">false</p>
          <p name="aoptdet">false</p>
          <p name="advcell">false</p>
            <list name="sibList">
            <item>
              <p name="sibcity">177</p>
              <p name="sibrep">2</p>
            </item>
            <item>
              <p name="sibcity">177</p>
              <p name="sibrep">1</p>
            </item>
          </list>
         </managedObject>
       <managedObject class="LN" distName="KRNS" id="93886">
          <p name="aak">150</p>
          <p name="orp">05</p>
          <p name="name">Portro</p>
          <p name="optres">false</p>
          <p name="optblu">tru</p>
          <p name="aoptdet">false</p>
          <p name="advcell">true</p>
        <list name="sibList">
            <item>
              <p name="sibcity">177</p>
              <p name="sibrep">1</p>
            </item>
            <item>
              <p name="sibcity">180</p>
              <p name="sibrep">2</p>
            </item>
          </list>
    </menagedObject>
    ....
    <menagedObject>
    ...
    </menagedObject>
    ...
</cmData>
</raml>


I need to go through all menagedObject and compare all parametars(p name). I wrote code for comparasion, but I don't know how to go through list and compare parametars, if they have same value, do nothing, if it's different, give output of different parameters and value. I wrote this function, where key is "p name" and value is value of "p name":

What I have tried:

Python
temp = []
for i in temp_ln:
    for j, k in zip(i.getchildren(), i):
        temp.append([i.get('distName'), j.get('name'), j.text])

    tempdict = {}
    for i in temp_ln:
        td = {}
        for j in i.getchildren():
            td.update({j.get('name'): j.text})
        tempdict.update({i.get('distName'): td})

        main_dif = {}
        for key, value in tempdict.iteritems():
            dif_k = {}
            for k, v in value.iteritems():
                try: 
                    a = ref[k]
                except:
                    a = None
                if v != a:
                    if k == 'name':
                        pass
                    else:
                        dif_k.update({k:(v, a)})
            main_dif.update({key:dif_k})
Posted
Updated 6-Mar-17 10:24am

1 solution

I made mistake with xml file, it doesn't look like that, missing all below.

XML
<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE raml SYSTEM 'raml20.dtd'>
    <raml version="2.0" xmlns="raml20.xsd">
    <cmData type="actual">
        <managedObject class="LN" distName="PTR" id="2425">
          <p name="aak">220</p>
          <p name="orp">05</p>
          <p name="name">Portro</p>
          <p name="optres">false</p>
          <p name="optblu">false</p>
          <p name="aoptdet">false</p>
          <p name="advcell">false</p>
            <list name="sibList">
            <item>
              <p name="sibcity">177</p>
              <p name="sibrep">2</p>
            </item>
            <item>
              <p name="sibcity">177</p>
              <p name="sibrep">1</p>
            </item>
          
         
       <managedObject class="LN" distName="KRNS" id="93886">
          <p name="aak">150</p>
          <p name="orp">05</p>
          <p name="name">Portro</p>
          <p name="optres">false</p>
          <p name="optblu">tru</p>
          <p name="aoptdet">false</p>
          <p name="advcell">true</p>
        <list name="sibList">
            <item>
              <p name="sibcity">177</p>
              <p name="sibrep">1</p>
            </item>
            <item>
              <p name="sibcity">180</p>
              <p name="sibrep">2</p>
            </item>
          
    
    ....
    <menagedobject>
    ...
    </menagedobject>
    ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900