Save XML in custom order of attribute in Python [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0
















This question already has an answer here:




  • Preserving XML attribute order?

    1 answer




I am trying to get specific order of XML attribute.



Given



<OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>


Required :



<OKV v="1" n="****" i="****" d="****" e="****" m="*****" g="****"  a="*****" r="*****" s="*****"/>


I am trying to implement in Python.



So far when I am using xml library.



import xml.etree.ElementTree as ET
tree = ET.parse('adhar.xml')


But it keeps read in that format,
Please tell how to change the order.
As I am reading correctly in internet explorer when opened the file, But getting reversed order while reading the file or see it in Chrome.










share|improve this question













marked as duplicate by kjhughes xml
Users with the  xml badge can single-handedly close xml questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 15:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

























    0
















    This question already has an answer here:




    • Preserving XML attribute order?

      1 answer




    I am trying to get specific order of XML attribute.



    Given



    <OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>


    Required :



    <OKV v="1" n="****" i="****" d="****" e="****" m="*****" g="****"  a="*****" r="*****" s="*****"/>


    I am trying to implement in Python.



    So far when I am using xml library.



    import xml.etree.ElementTree as ET
    tree = ET.parse('adhar.xml')


    But it keeps read in that format,
    Please tell how to change the order.
    As I am reading correctly in internet explorer when opened the file, But getting reversed order while reading the file or see it in Chrome.










    share|improve this question













    marked as duplicate by kjhughes xml
    Users with the  xml badge can single-handedly close xml questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Jan 4 at 15:58


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      0












      0








      0









      This question already has an answer here:




      • Preserving XML attribute order?

        1 answer




      I am trying to get specific order of XML attribute.



      Given



      <OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>


      Required :



      <OKV v="1" n="****" i="****" d="****" e="****" m="*****" g="****"  a="*****" r="*****" s="*****"/>


      I am trying to implement in Python.



      So far when I am using xml library.



      import xml.etree.ElementTree as ET
      tree = ET.parse('adhar.xml')


      But it keeps read in that format,
      Please tell how to change the order.
      As I am reading correctly in internet explorer when opened the file, But getting reversed order while reading the file or see it in Chrome.










      share|improve this question















      This question already has an answer here:




      • Preserving XML attribute order?

        1 answer




      I am trying to get specific order of XML attribute.



      Given



      <OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>


      Required :



      <OKV v="1" n="****" i="****" d="****" e="****" m="*****" g="****"  a="*****" r="*****" s="*****"/>


      I am trying to implement in Python.



      So far when I am using xml library.



      import xml.etree.ElementTree as ET
      tree = ET.parse('adhar.xml')


      But it keeps read in that format,
      Please tell how to change the order.
      As I am reading correctly in internet explorer when opened the file, But getting reversed order while reading the file or see it in Chrome.





      This question already has an answer here:




      • Preserving XML attribute order?

        1 answer








      python xml sorting encoding






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 4 at 6:56









      Chinmay DasChinmay Das

      929




      929




      marked as duplicate by kjhughes xml
      Users with the  xml badge can single-handedly close xml questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 4 at 15:58


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by kjhughes xml
      Users with the  xml badge can single-handedly close xml questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 4 at 15:58


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes


















          1














          For this specific xml string you could use xmltodict (install with pip install xmltodict). This converts the xml data to a python dictionary. Using the following would reverse the order of the OKV entry:



          import xmltodict
          import collections
          xml_data = '<OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>'
          xml_data_parsed = xmltodict.parse(xml_data)
          xml_data_parsed["OKV"] = collections.OrderedDict(reversed(list(xml_data_parsed["OKV"].items())))
          xml_data = xmltodict.unparse(xml_data_parsed)


          If the the part you want to reorder is embedded in a larger xml document you would have to select the deeper nested key to get to e.g. OKV.






          share|improve this answer

































            0














            Try using lxml to parse the xml file. As suggested here, it might help get the attributes in the appropriate order.






            share|improve this answer






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              For this specific xml string you could use xmltodict (install with pip install xmltodict). This converts the xml data to a python dictionary. Using the following would reverse the order of the OKV entry:



              import xmltodict
              import collections
              xml_data = '<OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>'
              xml_data_parsed = xmltodict.parse(xml_data)
              xml_data_parsed["OKV"] = collections.OrderedDict(reversed(list(xml_data_parsed["OKV"].items())))
              xml_data = xmltodict.unparse(xml_data_parsed)


              If the the part you want to reorder is embedded in a larger xml document you would have to select the deeper nested key to get to e.g. OKV.






              share|improve this answer






























                1














                For this specific xml string you could use xmltodict (install with pip install xmltodict). This converts the xml data to a python dictionary. Using the following would reverse the order of the OKV entry:



                import xmltodict
                import collections
                xml_data = '<OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>'
                xml_data_parsed = xmltodict.parse(xml_data)
                xml_data_parsed["OKV"] = collections.OrderedDict(reversed(list(xml_data_parsed["OKV"].items())))
                xml_data = xmltodict.unparse(xml_data_parsed)


                If the the part you want to reorder is embedded in a larger xml document you would have to select the deeper nested key to get to e.g. OKV.






                share|improve this answer




























                  1












                  1








                  1







                  For this specific xml string you could use xmltodict (install with pip install xmltodict). This converts the xml data to a python dictionary. Using the following would reverse the order of the OKV entry:



                  import xmltodict
                  import collections
                  xml_data = '<OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>'
                  xml_data_parsed = xmltodict.parse(xml_data)
                  xml_data_parsed["OKV"] = collections.OrderedDict(reversed(list(xml_data_parsed["OKV"].items())))
                  xml_data = xmltodict.unparse(xml_data_parsed)


                  If the the part you want to reorder is embedded in a larger xml document you would have to select the deeper nested key to get to e.g. OKV.






                  share|improve this answer















                  For this specific xml string you could use xmltodict (install with pip install xmltodict). This converts the xml data to a python dictionary. Using the following would reverse the order of the OKV entry:



                  import xmltodict
                  import collections
                  xml_data = '<OKV s="*****" r="*****" a="*****" g="****" m="*****" e="****" d="****" i="****" n="****" v="1"/>'
                  xml_data_parsed = xmltodict.parse(xml_data)
                  xml_data_parsed["OKV"] = collections.OrderedDict(reversed(list(xml_data_parsed["OKV"].items())))
                  xml_data = xmltodict.unparse(xml_data_parsed)


                  If the the part you want to reorder is embedded in a larger xml document you would have to select the deeper nested key to get to e.g. OKV.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 4 at 9:38

























                  answered Jan 4 at 7:34









                  AscurionAscurion

                  36519




                  36519

























                      0














                      Try using lxml to parse the xml file. As suggested here, it might help get the attributes in the appropriate order.






                      share|improve this answer




























                        0














                        Try using lxml to parse the xml file. As suggested here, it might help get the attributes in the appropriate order.






                        share|improve this answer


























                          0












                          0








                          0







                          Try using lxml to parse the xml file. As suggested here, it might help get the attributes in the appropriate order.






                          share|improve this answer













                          Try using lxml to parse the xml file. As suggested here, it might help get the attributes in the appropriate order.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 4 at 7:03









                          BabokBabok

                          466




                          466















                              Popular posts from this blog

                              Mossoró

                              Error while reading .h5 file using the rhdf5 package in R

                              Pushsharp Apns notification error: 'InvalidToken'