print the same level of xml data with a condition in python











up vote
0
down vote

favorite












I have an xml file. I didn't make this file so I don't want to make any changes. I want to print the screen by pulling the information that provides certain conditions from the file. The value I want to get is on the same level as the value I'll do with the comparison. So even if I can check the condition, I can't pull the other data. How can I do this?



asd.xml



<report>
<name>asd</name>
<description>example description - 1</description>
</report>
<report>
<name>dsa</name>
<description>example description - 2</description>
</report>
<report>
<name>asd</name>
<description>example description - 3</description>
</report>


asd.py



import xml.etree.ElementTree as ET
tree = ET.parse('asd.xml')
root = tree.getroot()

for report_1 in root:
for report_2 in report_1:
if result.tag == 'name':
if result.text == 'asd':
print ?


I find the ones that are equal to asd by retrieving the value from tag name "name". but I cannot access the same level decription value.



I want output



name: asd
description: example description - 1
name: asd
description: example description - 3









share|improve this question


























    up vote
    0
    down vote

    favorite












    I have an xml file. I didn't make this file so I don't want to make any changes. I want to print the screen by pulling the information that provides certain conditions from the file. The value I want to get is on the same level as the value I'll do with the comparison. So even if I can check the condition, I can't pull the other data. How can I do this?



    asd.xml



    <report>
    <name>asd</name>
    <description>example description - 1</description>
    </report>
    <report>
    <name>dsa</name>
    <description>example description - 2</description>
    </report>
    <report>
    <name>asd</name>
    <description>example description - 3</description>
    </report>


    asd.py



    import xml.etree.ElementTree as ET
    tree = ET.parse('asd.xml')
    root = tree.getroot()

    for report_1 in root:
    for report_2 in report_1:
    if result.tag == 'name':
    if result.text == 'asd':
    print ?


    I find the ones that are equal to asd by retrieving the value from tag name "name". but I cannot access the same level decription value.



    I want output



    name: asd
    description: example description - 1
    name: asd
    description: example description - 3









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have an xml file. I didn't make this file so I don't want to make any changes. I want to print the screen by pulling the information that provides certain conditions from the file. The value I want to get is on the same level as the value I'll do with the comparison. So even if I can check the condition, I can't pull the other data. How can I do this?



      asd.xml



      <report>
      <name>asd</name>
      <description>example description - 1</description>
      </report>
      <report>
      <name>dsa</name>
      <description>example description - 2</description>
      </report>
      <report>
      <name>asd</name>
      <description>example description - 3</description>
      </report>


      asd.py



      import xml.etree.ElementTree as ET
      tree = ET.parse('asd.xml')
      root = tree.getroot()

      for report_1 in root:
      for report_2 in report_1:
      if result.tag == 'name':
      if result.text == 'asd':
      print ?


      I find the ones that are equal to asd by retrieving the value from tag name "name". but I cannot access the same level decription value.



      I want output



      name: asd
      description: example description - 1
      name: asd
      description: example description - 3









      share|improve this question













      I have an xml file. I didn't make this file so I don't want to make any changes. I want to print the screen by pulling the information that provides certain conditions from the file. The value I want to get is on the same level as the value I'll do with the comparison. So even if I can check the condition, I can't pull the other data. How can I do this?



      asd.xml



      <report>
      <name>asd</name>
      <description>example description - 1</description>
      </report>
      <report>
      <name>dsa</name>
      <description>example description - 2</description>
      </report>
      <report>
      <name>asd</name>
      <description>example description - 3</description>
      </report>


      asd.py



      import xml.etree.ElementTree as ET
      tree = ET.parse('asd.xml')
      root = tree.getroot()

      for report_1 in root:
      for report_2 in report_1:
      if result.tag == 'name':
      if result.text == 'asd':
      print ?


      I find the ones that are equal to asd by retrieving the value from tag name "name". but I cannot access the same level decription value.



      I want output



      name: asd
      description: example description - 1
      name: asd
      description: example description - 3






      python xml






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 days ago









      Ali.Turkkan

      327




      327
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Your file produces an error:




          ParseError: junk after document element: line 5, column 0




          Should it look like this:



          <data>
          <report>
          <name>asd</name>
          <description>example description - 1</description>
          </report>
          <report>
          <name>dsa</name>
          <description>example description - 2</description>
          </report>
          <report>
          <name>asd</name>
          <description>example description - 3</description>
          </report>
          </data>


          You could use the following code:



          import xml.etree.ElementTree as ET

          tree = ET.parse('asd.xml')

          root = tree.getroot()

          flag = False

          for report in root:
          flag = False
          for row in report:
          if row.tag == 'name' and row.text == 'asd':
          print('name: asd')
          flag = True
          if flag and row.tag == 'description':
          print('description: {}'.format(row.text))


          That would provide you with desired result:



          name: asd
          description: example description - 1
          name: asd
          description: example description - 3





          share|improve this answer





















          • thank you. its's work fine.
            – Ali.Turkkan
            2 days ago


















          up vote
          1
          down vote













          What about this?



          asd.xml



          <?xml version="1.0" encoding="UTF-8"?>
          <root>
          <report>
          <name>asd</name>
          <description>example description - 1</description>
          </report>
          <report>
          <name>dsa</name>
          <description>example description - 2</description>
          </report>
          <report>
          <name>asd</name>
          <description>example description - 3</description>
          </report>
          </root>


          Python3



          import xml.etree.ElementTree as ET
          tree = ET.parse(r"asd.xml")
          root = tree.getroot()

          for report in root:
          if report.find('name').text == "asd":
          print("name: ", report.find('name').text)
          print("description: ", report.find('description').text)





          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%2f53372453%2fprint-the-same-level-of-xml-data-with-a-condition-in-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            Your file produces an error:




            ParseError: junk after document element: line 5, column 0




            Should it look like this:



            <data>
            <report>
            <name>asd</name>
            <description>example description - 1</description>
            </report>
            <report>
            <name>dsa</name>
            <description>example description - 2</description>
            </report>
            <report>
            <name>asd</name>
            <description>example description - 3</description>
            </report>
            </data>


            You could use the following code:



            import xml.etree.ElementTree as ET

            tree = ET.parse('asd.xml')

            root = tree.getroot()

            flag = False

            for report in root:
            flag = False
            for row in report:
            if row.tag == 'name' and row.text == 'asd':
            print('name: asd')
            flag = True
            if flag and row.tag == 'description':
            print('description: {}'.format(row.text))


            That would provide you with desired result:



            name: asd
            description: example description - 1
            name: asd
            description: example description - 3





            share|improve this answer





















            • thank you. its's work fine.
              – Ali.Turkkan
              2 days ago















            up vote
            1
            down vote



            accepted










            Your file produces an error:




            ParseError: junk after document element: line 5, column 0




            Should it look like this:



            <data>
            <report>
            <name>asd</name>
            <description>example description - 1</description>
            </report>
            <report>
            <name>dsa</name>
            <description>example description - 2</description>
            </report>
            <report>
            <name>asd</name>
            <description>example description - 3</description>
            </report>
            </data>


            You could use the following code:



            import xml.etree.ElementTree as ET

            tree = ET.parse('asd.xml')

            root = tree.getroot()

            flag = False

            for report in root:
            flag = False
            for row in report:
            if row.tag == 'name' and row.text == 'asd':
            print('name: asd')
            flag = True
            if flag and row.tag == 'description':
            print('description: {}'.format(row.text))


            That would provide you with desired result:



            name: asd
            description: example description - 1
            name: asd
            description: example description - 3





            share|improve this answer





















            • thank you. its's work fine.
              – Ali.Turkkan
              2 days ago













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            Your file produces an error:




            ParseError: junk after document element: line 5, column 0




            Should it look like this:



            <data>
            <report>
            <name>asd</name>
            <description>example description - 1</description>
            </report>
            <report>
            <name>dsa</name>
            <description>example description - 2</description>
            </report>
            <report>
            <name>asd</name>
            <description>example description - 3</description>
            </report>
            </data>


            You could use the following code:



            import xml.etree.ElementTree as ET

            tree = ET.parse('asd.xml')

            root = tree.getroot()

            flag = False

            for report in root:
            flag = False
            for row in report:
            if row.tag == 'name' and row.text == 'asd':
            print('name: asd')
            flag = True
            if flag and row.tag == 'description':
            print('description: {}'.format(row.text))


            That would provide you with desired result:



            name: asd
            description: example description - 1
            name: asd
            description: example description - 3





            share|improve this answer












            Your file produces an error:




            ParseError: junk after document element: line 5, column 0




            Should it look like this:



            <data>
            <report>
            <name>asd</name>
            <description>example description - 1</description>
            </report>
            <report>
            <name>dsa</name>
            <description>example description - 2</description>
            </report>
            <report>
            <name>asd</name>
            <description>example description - 3</description>
            </report>
            </data>


            You could use the following code:



            import xml.etree.ElementTree as ET

            tree = ET.parse('asd.xml')

            root = tree.getroot()

            flag = False

            for report in root:
            flag = False
            for row in report:
            if row.tag == 'name' and row.text == 'asd':
            print('name: asd')
            flag = True
            if flag and row.tag == 'description':
            print('description: {}'.format(row.text))


            That would provide you with desired result:



            name: asd
            description: example description - 1
            name: asd
            description: example description - 3






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 days ago









            zipa

            15.5k21334




            15.5k21334












            • thank you. its's work fine.
              – Ali.Turkkan
              2 days ago


















            • thank you. its's work fine.
              – Ali.Turkkan
              2 days ago
















            thank you. its's work fine.
            – Ali.Turkkan
            2 days ago




            thank you. its's work fine.
            – Ali.Turkkan
            2 days ago












            up vote
            1
            down vote













            What about this?



            asd.xml



            <?xml version="1.0" encoding="UTF-8"?>
            <root>
            <report>
            <name>asd</name>
            <description>example description - 1</description>
            </report>
            <report>
            <name>dsa</name>
            <description>example description - 2</description>
            </report>
            <report>
            <name>asd</name>
            <description>example description - 3</description>
            </report>
            </root>


            Python3



            import xml.etree.ElementTree as ET
            tree = ET.parse(r"asd.xml")
            root = tree.getroot()

            for report in root:
            if report.find('name').text == "asd":
            print("name: ", report.find('name').text)
            print("description: ", report.find('description').text)





            share|improve this answer

























              up vote
              1
              down vote













              What about this?



              asd.xml



              <?xml version="1.0" encoding="UTF-8"?>
              <root>
              <report>
              <name>asd</name>
              <description>example description - 1</description>
              </report>
              <report>
              <name>dsa</name>
              <description>example description - 2</description>
              </report>
              <report>
              <name>asd</name>
              <description>example description - 3</description>
              </report>
              </root>


              Python3



              import xml.etree.ElementTree as ET
              tree = ET.parse(r"asd.xml")
              root = tree.getroot()

              for report in root:
              if report.find('name').text == "asd":
              print("name: ", report.find('name').text)
              print("description: ", report.find('description').text)





              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                What about this?



                asd.xml



                <?xml version="1.0" encoding="UTF-8"?>
                <root>
                <report>
                <name>asd</name>
                <description>example description - 1</description>
                </report>
                <report>
                <name>dsa</name>
                <description>example description - 2</description>
                </report>
                <report>
                <name>asd</name>
                <description>example description - 3</description>
                </report>
                </root>


                Python3



                import xml.etree.ElementTree as ET
                tree = ET.parse(r"asd.xml")
                root = tree.getroot()

                for report in root:
                if report.find('name').text == "asd":
                print("name: ", report.find('name').text)
                print("description: ", report.find('description').text)





                share|improve this answer












                What about this?



                asd.xml



                <?xml version="1.0" encoding="UTF-8"?>
                <root>
                <report>
                <name>asd</name>
                <description>example description - 1</description>
                </report>
                <report>
                <name>dsa</name>
                <description>example description - 2</description>
                </report>
                <report>
                <name>asd</name>
                <description>example description - 3</description>
                </report>
                </root>


                Python3



                import xml.etree.ElementTree as ET
                tree = ET.parse(r"asd.xml")
                root = tree.getroot()

                for report in root:
                if report.find('name').text == "asd":
                print("name: ", report.find('name').text)
                print("description: ", report.find('description').text)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 days ago









                Ettore Rizza

                1,8502617




                1,8502617






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372453%2fprint-the-same-level-of-xml-data-with-a-condition-in-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

                    Volksrepublik China

                    How to test boost logger output in unit testing?

                    How do you send bulk inserts with no ids to elasticsearch