Extracting a value from an xml file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ironmonkey69
    New Member
    • Jul 2007
    • 43

    Extracting a value from an xml file

    Is there a way to use python to extract a value from an xml file?
  • KaezarRex
    New Member
    • Sep 2007
    • 52

    #2
    Originally posted by ironmonkey69
    Is there a way to use python to extract a value from an xml file?
    There are many different ways to parse xml in python. I would suggest looking into a module called minidom in the python library reference. They have a simple example to help you get started.
    Last edited by KaezarRex; Sep 20 '07, 06:44 PM. Reason: corrected link

    Comment

    • ironmonkey69
      New Member
      • Jul 2007
      • 43

      #3
      I can use the code below to parse the file, but how can I extract a value of a tag such as 'name' with a value and return that value to an external program?
      Code:
      from xml.dom.minidom import parse, parseString
      dom1 = parse('c:\\temp\\mydata.xml') # parse an XML file by name

      Comment

      • ironmonkey69
        New Member
        • Jul 2007
        • 43

        #4
        this is what the contents of the xml file looks like and I want to take the value of 'name' and return that to an external program.

        Code:
        <servicing name="color-measure-print-and-scan">

        Comment

        • KaezarRex
          New Member
          • Sep 2007
          • 52

          #5
          Originally posted by ironmonkey69
          this is what the contents of the xml file looks like and I want to take the value of 'name' and return that to an external program.

          Code:
          <servicing name="color-measure-print-and-scan">

          [CODE=python]elements = dom1.getElement sByTagName("ser vicing")
          return elements[0].attributes['name'].value[/CODE]
          If you have multiple servicing tags, just iterate through elements instead of only looking at the first index.

          Comment

          • ironmonkey69
            New Member
            • Jul 2007
            • 43

            #6
            My python script looks like this

            Code:
            import xml.dom.minidom
            
            from xml.dom.minidom import parse, parseString
            dom1 = parse('/home/test.xml') # parse an XML file by name
            elements = dom1.getElementsByTagName("servicing")
            	return elements[0].attributes['name'].value
            I get an error that says that return is outside function. I am using python 2.0.

            Comment

            • KaezarRex
              New Member
              • Sep 2007
              • 52

              #7
              Originally posted by ironmonkey69
              My python script looks like this

              Code:
              import xml.dom.minidom
              
              from xml.dom.minidom import parse, parseString
              dom1 = parse('/home/test.xml') # parse an XML file by name
              elements = dom1.getElementsByTagName("servicing")
              	return elements[0].attributes['name'].value
              I get an error that says that return is outside function. I am using python 2.0.
              Sorry, I misunderstood what you mean by returning the value to an external program.
              Try this:
              [CODE=python]import xml.dom.minidom
              from xml.dom.minidom import parse, parseString
              dom1 = parse('/home/test.xml')
              elements = dom1.getElement sByTagName("ser vicing")
              name = elements[0].attributes['name'].value
              print name[/CODE]

              Comment

              • ironmonkey69
                New Member
                • Jul 2007
                • 43

                #8
                when i run this script, it says "ImportErro r: No module named xml.dom.minidom "
                Is there any other way to perform this function without using minidom?

                Comment

                • KaezarRex
                  New Member
                  • Sep 2007
                  • 52

                  #9
                  Originally posted by ironmonkey69
                  when i run this script, it says "ImportErro r: No module named xml.dom.minidom "
                  Is there any other way to perform this function without using minidom?
                  That's strange that it won't import because minidom is automatically included in python. I am using 2.5, but I'm almost positive that the minidom module should work the same as in 2.0.
                  As far as other ways to parse go, I have only used minidom to extract info from xml files, so I don't know of any other way to recommend.

                  Comment

                  • bartonc
                    Recognized Expert Expert
                    • Sep 2006
                    • 6478

                    #10
                    Originally posted by KaezarRex
                    That's strange that it won't import because minidom is automatically included in python. I am using 2.5, but I'm almost positive that the minidom module should work the same as in 2.0.
                    As far as other ways to parse go, I have only used minidom to extract info from xml files, so I don't know of any other way to recommend.
                    Yep. And 2.4.4 the docs chapter is "13.7 xml.dom.minidom ".

                    Comment

                    • bartonc
                      Recognized Expert Expert
                      • Sep 2006
                      • 6478

                      #11
                      Originally posted by ironmonkey69
                      when i run this script, it says "ImportErro r: No module named xml.dom.minidom "
                      Is there any other way to perform this function without using minidom?
                      This also makes no sense to me because you made it past the import in your reply #6. Something has changed since then. Can you think what that might be?

                      Comment

                      • ironmonkey69
                        New Member
                        • Jul 2007
                        • 43

                        #12
                        Is there a way to parse the xml file using the xmllib?

                        Comment

                        • ghostdog74
                          Recognized Expert Contributor
                          • Apr 2006
                          • 511

                          #13
                          Originally posted by ironmonkey69
                          this is what the contents of the xml file looks like and I want to take the value of 'name' and return that to an external program.

                          Code:
                          <servicing name="color-measure-print-and-scan">
                          its only 1 line? can you show more sample of your xml file, if any?

                          Comment

                          • ironmonkey69
                            New Member
                            • Jul 2007
                            • 43

                            #14
                            sure

                            [CODE=xml]
                            <servicing name="alignment ">

                            <!-- *************** *************** ****** BEGIN OF PASS *************** *************** ************ -->

                            <!-- TRJ-002448 Changed to prevent the carriage from going to the secondary spittoon during printhead alignment
                            plots. This happens because the printmode is a mix of uni-directional and bi-directional print modes. Date 11th April 2006 -->
                            <begin-of-pass>
                            <flying-spit type="fixed">
                            <parameters>
                            <fixed mode="only-SVS"/>
                            </parameters>
                            </flying-spit>
                            </begin-of-pass>


                            <!-- *************** *************** ****** BEGIN OF PAGE *************** *************** ************ -->

                            <!-- Disable drop detection -->
                            <begin-of-page>
                            <drop-detection enabled="0">
                            </drop-detection>
                            </begin-of-page>


                            <!-- *************** *************** ****** END OF JOB *************** *************** ************ -->

                            <!-- Disable drop detection -->
                            <end-of-job>
                            <drop-detection enabled="0">
                            </drop-detection>
                            </end-of-job>

                            </servicing>
                            [/CODE]
                            Last edited by bartonc; Sep 25 '07, 02:53 PM. Reason: Added =sml to the [CODE] tag

                            Comment

                            • bvdet
                              Recognized Expert Specialist
                              • Oct 2006
                              • 2851

                              #15
                              Originally posted by ironmonkey69
                              sure

                              [CODE=xml]
                              <servicing name="alignment ">

                              <!-- *************** *************** ****** BEGIN OF PASS *************** *************** ************ -->

                              <!-- TRJ-002448 Changed to prevent the carriage from going to the secondary spittoon during printhead alignment
                              plots. This happens because the printmode is a mix of uni-directional and bi-directional print modes. Date 11th April 2006 -->
                              <begin-of-pass>
                              <flying-spit type="fixed">
                              <parameters>
                              <fixed mode="only-SVS"/>
                              </parameters>
                              </flying-spit>
                              </begin-of-pass>


                              <!-- *************** *************** ****** BEGIN OF PAGE *************** *************** ************ -->

                              <!-- Disable drop detection -->
                              <begin-of-page>
                              <drop-detection enabled="0">
                              </drop-detection>
                              </begin-of-page>


                              <!-- *************** *************** ****** END OF JOB *************** *************** ************ -->

                              <!-- Disable drop detection -->
                              <end-of-job>
                              <drop-detection enabled="0">
                              </drop-detection>
                              </end-of-job>

                              </servicing>
                              [/CODE]
                              If all you want to do is read that one value, you might consider a regex solution:[code=Python]import re

                              keyword = 'servicing'
                              patt = re.compile(r'<% s(.*)>' % keyword)

                              s = patt.search(ope n('file_name.xm l').read()).gro up(1)
                              if '=' in s:
                              print s.split('=')[1][/code]

                              Output:
                              >>> "alignment"

                              Comment

                              Working...