473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Get XML tag values with Python

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
1 5074
Laharl
849 Expert 512MB
Look at the xml.dom.minidom.Element's getAttribute method...(Node is a subclass of Element, don't worry).
Nov 20 '08 #2

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

Similar topics

2
by: Christopher Kang | last post by:
I had a question about python and mysql, but I want to first thank those who taught me about the factory method, it was exactly what I needed. Anyway, I have a problem where I am pulling...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
3
by: Elezar Simeon Papo | last post by:
Hello All, I have a tab separated input file (data.txt) in text format - the file looks like this SCHOOL DEPART1 DEPART2 DEPART3 Harvard Economics Mathematics Physics...
3
by: bolly | last post by:
Hi, I've been putting Python data into a sqlite3 database as tuples but when I retrieve them they come back as unicode data e.g 'u(1,2,3,4)'.How can I change it back to a tuple so I can use it as...
16
by: Preben Randhol | last post by:
Hi A short newbie question. I would like to extract some values from a given text file directly into python variables. Can this be done simply by either standard library or other libraries? Some...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
3
by: prouleau001 | last post by:
Hi all, While trying to use simplejson under Python 2.4.3 I have been investigating the handling of special floating point values and found that both Python 2.4 and 2.5 return False when...
4
by: McA | last post by:
Hi all, probably a dumb question, but I didn't find something elegant for my problem so far. In perl you can unpack the element of a list to variables similar as in python (a, b, c = ), but...
0
by: Maric Michaud | last post by:
Le Thursday 28 August 2008 03:43:16 norseman, vous avez écrit : Disctionaries are hash tables with a unique key and constant time lookup. What you want could be implemented as a complex data...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.