By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

Get XML tag values with Python

P: 34
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.

Expand|Select|Wrap|Line Numbers
  1. # Import system modules
  2. import sys, string, os, xml.dom.minidom
  3. from xml.dom.minidom import parse, parseString
  4.  
  5. metadata_xml_file = "C:\\Temp\\county_simp2.shp.xml"
  6.  
  7. parseXMLdoc = parse(metadata_xml_file)
  8.  
  9. elements = parseXMLdoc.getElementsByTagName("abstract")
  10.  
  11. xValue = elements[3].attributes['abstract'].value
  12. print xValue
  13.  
  14. ##openFile = open(metadata_xml_file, "r")
  15. ##
  16. ##line = openFile.readline()
  17. ##
  18. ##checkList = ["abstract", "purpose", "origin", "pubdate", "current", "caldate", "progress", "update"]
  19. ##
  20. ##print checkList[0]
  21. ####
  22. ##while line:
  23. ##    if checkList[0] in line:
  24. ##        while ("/" + checkList[0]) not in line:
  25. ##            start, end = line.find(">"), line.find("<")
  26. ##            value = line[start+1:end]
  27. ##
  28. ##            if value == "":
  29. ##                print checklist[0] + " item has no value!"
  30. ##
  31. ##            if value == "REQUIRED:":
  32. ##                print checklist[0] + " item must be chnaged!"
  33. ##
  34. ##
  35. print "done!"
Thanks in advance for any guidance!

**[HTML]
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
- <metadata>
- <Esri>
<CreaDate>20070606</CreaDate>
<CreaTime>11054200</CreaTime>
<SyncOnce>FALSE</SyncOnce>
<SyncDate>20081119</SyncDate>
<SyncTime>14174300</SyncTime>
<ModDate>20081119</ModDate>
<ModTime>14192600</ModTime>
- <DataProperties>
- <lineage>
<Process Name="SimplifyPolygon_1" ToolSource="C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Data Management Tools.tbx\SimplifyPolygon" Date="20060623" Time="111251">SimplifyPolygon COUNTY C:\projects\SP6\district_gen.mdb\COUNTY_SimplifyPo lygon POINT_REMOVE "0.0003 DecimalDegrees" "0 Unknown" PRESERVE_SHARED NO_KEEP C:\projects\SP6\district_gen.mdb\COUNTY_SimplifyPo lygon_Pnt</Process>
<Process Name="FeatureClassToFeatureClass_1" ToolSource="F:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Conversion Tools.tbx\FeatureClassToFeatureClass" Date="20060624" Time="120113">Connections\Connection to jenner.phsc.esri.com.sde\TRACKING.COUNTY_SIMP"</Process>
<Process Name="FeatureClassToFeatureClass_1" ToolSource="C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Conversion Tools.tbx\FeatureClassToFeatureClass" Date="20070605" Time="103617">"C:\Documents and Settings\jenn5214\My Documents\Projects\sweat reports\features\county_simp.shp"</Process>
<Process Name="Project_11" ToolSource="C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Data Management Tools.tbx\Project" Date="20070606" Time="110543">Project "C:\Documents and Settings\jenn5214\My Documents\Projects\SWEAT reports\features\county_simp.shp"</Process>
</lineage>
</DataProperties>
<MetaID>{A0F30B95-144D-495E-B754-5EA3274FFBD1}</MetaID>
</Esri>
- <idinfo>
<native Sync="TRUE">Microsoft Windows XP Version 5.1 (Build 2600) Service Pack 2; ESRI ArcCatalog 9.2.5.1450</native>
- <descript>
<langdata Sync="TRUE">en</langdata>
<abstract>test jenn added wed</abstract>
<purpose>REQUIRED: A summary of the intentions with which the data set was developed.</purpose>
</descript>
- <citation>
- <citeinfo>
<origin>REQUIRED: The name of an organization or individual that developed the data set.</origin>
<pubdate>REQUIRED: The date when the data set is published or otherwise made available for release.</pubdate>
<title Sync="TRUE">county_simp2</title>
<ftname Sync="TRUE">county_simp2</ftname>
<geoform Sync="TRUE">vector digital data</geoform>
<onlink Sync="TRUE">\\JDUERR\C$\Documents and Settings\jenn5214\My Documents\Projects\SWEAT reports\features\county_simp2.shp</onlink>
</citeinfo>
</citation>
...
[/HTML]
Nov 20 '08 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 849
Look at the xml.dom.minidom.Element's getAttribute method...(Node is a subclass of Element, don't worry).
Nov 20 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.