Read an xml element using python











up vote
-1
down vote

favorite












I have an xml file. I want to search for a specific word in the file, and if i find it- i want to copy all of the xml element the word was in it.



for example:



 <Actions>
<ActionGroup enabled="yes" name="viewsGroup" isExclusive="yes"/>
<ExtAction iconSet="" toolTip="" name="f5-script" text="f5-script"/>
</Actions>


I am looking for the word :"ExtAction", and since it is inside the Actions element I want to copy all of it. How can I do it?










share|improve this question




























    up vote
    -1
    down vote

    favorite












    I have an xml file. I want to search for a specific word in the file, and if i find it- i want to copy all of the xml element the word was in it.



    for example:



     <Actions>
    <ActionGroup enabled="yes" name="viewsGroup" isExclusive="yes"/>
    <ExtAction iconSet="" toolTip="" name="f5-script" text="f5-script"/>
    </Actions>


    I am looking for the word :"ExtAction", and since it is inside the Actions element I want to copy all of it. How can I do it?










    share|improve this question


























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I have an xml file. I want to search for a specific word in the file, and if i find it- i want to copy all of the xml element the word was in it.



      for example:



       <Actions>
      <ActionGroup enabled="yes" name="viewsGroup" isExclusive="yes"/>
      <ExtAction iconSet="" toolTip="" name="f5-script" text="f5-script"/>
      </Actions>


      I am looking for the word :"ExtAction", and since it is inside the Actions element I want to copy all of it. How can I do it?










      share|improve this question















      I have an xml file. I want to search for a specific word in the file, and if i find it- i want to copy all of the xml element the word was in it.



      for example:



       <Actions>
      <ActionGroup enabled="yes" name="viewsGroup" isExclusive="yes"/>
      <ExtAction iconSet="" toolTip="" name="f5-script" text="f5-script"/>
      </Actions>


      I am looking for the word :"ExtAction", and since it is inside the Actions element I want to copy all of it. How can I do it?







      python xml






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 16:00









      narendra-choudhary

      2,19121631




      2,19121631










      asked Nov 10 at 14:52









      ASOLOMO

      123




      123
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I usually use ElementTree for this kind of job, as it seems the most intuitive to me. I believe this is part of the standard library, so no need to install anything



          As a more general approach, the entire .xml file can be parsed as a dictionary of dictionaries, which you can then index accordingly if you so desired. This can be done like this (I just made a copy of your .xml file locally and called it "test.xml" for demonstration purposes. Of course, change this to correspond to your file if you choose this solution):



          import xml.etree.ElementTree as ET

          tree = ET.parse('test.xml')
          root = tree.getroot()

          tags = [child.tag for child in root]
          file_contents = {}
          for tag in tags:
          for p in tree.iter(tag=tag):
          file_contents[tag] = dict(p.items())


          If you print the file contents you will get:



          "{'ActionGroup': {'enabled': 'yes', 'name': 'viewsGroup', 'isExclusive': 'yes'}, 'ExtAction': {'iconSet': '', 'toolTip': '', 'name': 'f5-script', 'text': 'f5-script'}}"



          From this it is trivial to index out the bits of information you need. For example, if you want to get the name value from the ExtAction tag, you would just do:



          print(file_contents['ExtAction']['name'])  # or save this as a variable if you need it


          Hope this helps!






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240127%2fread-an-xml-element-using-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            I usually use ElementTree for this kind of job, as it seems the most intuitive to me. I believe this is part of the standard library, so no need to install anything



            As a more general approach, the entire .xml file can be parsed as a dictionary of dictionaries, which you can then index accordingly if you so desired. This can be done like this (I just made a copy of your .xml file locally and called it "test.xml" for demonstration purposes. Of course, change this to correspond to your file if you choose this solution):



            import xml.etree.ElementTree as ET

            tree = ET.parse('test.xml')
            root = tree.getroot()

            tags = [child.tag for child in root]
            file_contents = {}
            for tag in tags:
            for p in tree.iter(tag=tag):
            file_contents[tag] = dict(p.items())


            If you print the file contents you will get:



            "{'ActionGroup': {'enabled': 'yes', 'name': 'viewsGroup', 'isExclusive': 'yes'}, 'ExtAction': {'iconSet': '', 'toolTip': '', 'name': 'f5-script', 'text': 'f5-script'}}"



            From this it is trivial to index out the bits of information you need. For example, if you want to get the name value from the ExtAction tag, you would just do:



            print(file_contents['ExtAction']['name'])  # or save this as a variable if you need it


            Hope this helps!






            share|improve this answer

























              up vote
              0
              down vote













              I usually use ElementTree for this kind of job, as it seems the most intuitive to me. I believe this is part of the standard library, so no need to install anything



              As a more general approach, the entire .xml file can be parsed as a dictionary of dictionaries, which you can then index accordingly if you so desired. This can be done like this (I just made a copy of your .xml file locally and called it "test.xml" for demonstration purposes. Of course, change this to correspond to your file if you choose this solution):



              import xml.etree.ElementTree as ET

              tree = ET.parse('test.xml')
              root = tree.getroot()

              tags = [child.tag for child in root]
              file_contents = {}
              for tag in tags:
              for p in tree.iter(tag=tag):
              file_contents[tag] = dict(p.items())


              If you print the file contents you will get:



              "{'ActionGroup': {'enabled': 'yes', 'name': 'viewsGroup', 'isExclusive': 'yes'}, 'ExtAction': {'iconSet': '', 'toolTip': '', 'name': 'f5-script', 'text': 'f5-script'}}"



              From this it is trivial to index out the bits of information you need. For example, if you want to get the name value from the ExtAction tag, you would just do:



              print(file_contents['ExtAction']['name'])  # or save this as a variable if you need it


              Hope this helps!






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I usually use ElementTree for this kind of job, as it seems the most intuitive to me. I believe this is part of the standard library, so no need to install anything



                As a more general approach, the entire .xml file can be parsed as a dictionary of dictionaries, which you can then index accordingly if you so desired. This can be done like this (I just made a copy of your .xml file locally and called it "test.xml" for demonstration purposes. Of course, change this to correspond to your file if you choose this solution):



                import xml.etree.ElementTree as ET

                tree = ET.parse('test.xml')
                root = tree.getroot()

                tags = [child.tag for child in root]
                file_contents = {}
                for tag in tags:
                for p in tree.iter(tag=tag):
                file_contents[tag] = dict(p.items())


                If you print the file contents you will get:



                "{'ActionGroup': {'enabled': 'yes', 'name': 'viewsGroup', 'isExclusive': 'yes'}, 'ExtAction': {'iconSet': '', 'toolTip': '', 'name': 'f5-script', 'text': 'f5-script'}}"



                From this it is trivial to index out the bits of information you need. For example, if you want to get the name value from the ExtAction tag, you would just do:



                print(file_contents['ExtAction']['name'])  # or save this as a variable if you need it


                Hope this helps!






                share|improve this answer












                I usually use ElementTree for this kind of job, as it seems the most intuitive to me. I believe this is part of the standard library, so no need to install anything



                As a more general approach, the entire .xml file can be parsed as a dictionary of dictionaries, which you can then index accordingly if you so desired. This can be done like this (I just made a copy of your .xml file locally and called it "test.xml" for demonstration purposes. Of course, change this to correspond to your file if you choose this solution):



                import xml.etree.ElementTree as ET

                tree = ET.parse('test.xml')
                root = tree.getroot()

                tags = [child.tag for child in root]
                file_contents = {}
                for tag in tags:
                for p in tree.iter(tag=tag):
                file_contents[tag] = dict(p.items())


                If you print the file contents you will get:



                "{'ActionGroup': {'enabled': 'yes', 'name': 'viewsGroup', 'isExclusive': 'yes'}, 'ExtAction': {'iconSet': '', 'toolTip': '', 'name': 'f5-script', 'text': 'f5-script'}}"



                From this it is trivial to index out the bits of information you need. For example, if you want to get the name value from the ExtAction tag, you would just do:



                print(file_contents['ExtAction']['name'])  # or save this as a variable if you need it


                Hope this helps!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 15:57









                J.Aluko

                163




                163






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240127%2fread-an-xml-element-using-python%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues