Get XML tag values with Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jld730
    New Member
    • Apr 2007
    • 34

    Get XML tag values with Python

    If I wanted to get at the value in the tags for 'abstract' and 'purpose' and 'origin' in the below/end xml file, how would I got about doing that? I have tried these two ways, no luck.

    Code:
    # Import system modules
    import sys, string, os, xml.dom.minidom
    from xml.dom.minidom import parse, parseString
    
    metadata_xml_file = "C:\\Temp\\county_simp2.shp.xml"
    
    parseXMLdoc = parse(metadata_xml_file)
    
    elements = parseXMLdoc.getElementsByTagName("abstract")
    
    xValue = elements[3].attributes['abstract'].value
    print xValue
    
    ##openFile = open(metadata_xml_file, "r")
    ##
    ##line = openFile.readline()
    ##
    ##checkList = ["abstract", "purpose", "origin", "pubdate", "current", "caldate", "progress", "update"]
    ##
    ##print checkList[0]
    ####
    ##while line:
    ##    if checkList[0] in line:
    ##        while ("/" + checkList[0]) not in line:
    ##            start, end = line.find(">"), line.find("<")
    ##            value = line[start+1:end]
    ##
    ##            if value == "":
    ##                print checklist[0] + " item has no value!"
    ##
    ##            if value == "REQUIRED:":
    ##                print checklist[0] + " item must be chnaged!"
    ##
    ##
    print "done!"
    Thanks in advance for any guidance!

    **[HTML]
    <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    - <metadata>
    - <Esri>
    <CreaDate>20070 606</CreaDate>
    <CreaTime>11054 200</CreaTime>
    <SyncOnce>FALSE </SyncOnce>
    <SyncDate>20081 119</SyncDate>
    <SyncTime>14174 300</SyncTime>
    <ModDate>200811 19</ModDate>
    <ModTime>141926 00</ModTime>
    - <DataProperties >
    - <lineage>
    <Process Name="SimplifyP olygon_1" ToolSource="C:\ Program Files\ArcGIS\Ar cToolbox\Toolbo xes\Data Management Tools.tbx\Simpl ifyPolygon" Date="20060623" Time="111251">S implifyPolygon COUNTY C:\projects\SP6 \district_gen.m db\COUNTY_Simpl ifyPolygon POINT_REMOVE "0.0003 DecimalDegrees" "0 Unknown" PRESERVE_SHARED NO_KEEP C:\projects\SP6 \district_gen.m db\COUNTY_Simpl ifyPolygon_Pnt</Process>
    <Process Name="FeatureCl assToFeatureCla ss_1" ToolSource="F:\ Program Files\ArcGIS\Ar cToolbox\Toolbo xes\Conversion Tools.tbx\Featu reClassToFeatur eClass" Date="20060624" Time="120113">C onnections\Conn ection to jenner.phsc.esr i.com.sde\TRACK ING.COUNTY_SIMP "</Process>
    <Process Name="FeatureCl assToFeatureCla ss_1" ToolSource="C:\ Program Files\ArcGIS\Ar cToolbox\Toolbo xes\Conversion Tools.tbx\Featu reClassToFeatur eClass" Date="20070605" Time="103617">" C:\Documents and Settings\jenn52 14\My Documents\Proje cts\sweat reports\feature s\county_simp.s hp"</Process>
    <Process Name="Project_1 1" ToolSource="C:\ Program Files\ArcGIS\Ar cToolbox\Toolbo xes\Data Management Tools.tbx\Proje ct" Date="20070606" Time="110543">P roject "C:\Documen ts and Settings\jenn52 14\My Documents\Proje cts\SWEAT reports\feature s\county_simp.s hp"</Process>
    </lineage>
    </DataProperties>
    <MetaID>{A0F30B 95-144D-495E-B754-5EA3274FFBD1}</MetaID>
    </Esri>
    - <idinfo>
    <native Sync="TRUE">Mic rosoft Windows XP Version 5.1 (Build 2600) Service Pack 2; ESRI ArcCatalog 9.2.5.1450</native>
    - <descript>
    <langdata Sync="TRUE">en</langdata>
    <abstract>tes t jenn added wed</abstract>
    <purpose>REQUIR ED: A summary of the intentions with which the data set was developed.</purpose>
    </descript>
    - <citation>
    - <citeinfo>
    <origin>REQUIRE D: The name of an organization or individual that developed the data set.</origin>
    <pubdate>REQUIR ED: The date when the data set is published or otherwise made available for release.</pubdate>
    <title Sync="TRUE">cou nty_simp2</title>
    <ftname Sync="TRUE">cou nty_simp2</ftname>
    <geoform Sync="TRUE">vec tor digital data</geoform>
    <onlink Sync="TRUE">\\J DUERR\C$\Docume nts and Settings\jenn52 14\My Documents\Proje cts\SWEAT reports\feature s\county_simp2. shp</onlink>
    </citeinfo>
    </citation>
    ...
    [/HTML]
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Look at the xml.dom.minidom .Element's getAttribute method...(Node is a subclass of Element, don't worry).

    Comment

    Working...