simple XML question | Member | | Join Date: Jun 2007
Posts: 66
| | |
i know it's quite simple, but i looked around for hours and i dont seem to be able to get it, excuse my nubiness pleaseeee
i need to parse an xml document, to take out the values inside since i'm using it as a database,
here's a sample xml file
<?xml version="1.0" ?><amman Address="3rd area" Hazard="weapons" Name="third" Owner="tarik" Phone="6453222" PhotoAddress="C:\Users\tarik\Desktop\DSC_0018.jpg" PrevUsed="project1" />
i need to extract the values just as a string, so if i ask for address ill just get 3rd area
supposing the XML file is located at c:/tempDB
eternally grateful for an answer!!
T
| | Newbie | | Join Date: Aug 2009 Location: Louisville
Posts: 13
| | | re: simple XML question
I'm not familiar with XML myself but I think this should get you started.
Using xml.dom.minidom ( http://docs.python.org/library/xml.dom.minidom.html) you can parse a file which should return a Document object which is documented here. You can then (I believe) use the Document methods to grab elements and values and the like.
Hope this helps!
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,563
| | | re: simple XML question
I have done some XML parsing, and it was not simple to me! The following will parse the document into a dictionary: - from xml.dom.minidom import parseString
-
-
xmlStr = '''<?xml version="1.0" ?>
-
<amman Address="3rd area"
-
Hazard="weapons"
-
Name="third"
-
Owner="tarik"
-
Phone="6453222"
-
PhotoAddress="C:\Users\tarik\Desktop\DSC_0018.jpg"
-
PrevUsed="project1" />'''
-
-
xmlDoc = parseString(xmlStr)
-
-
def getNodeDict(doc, dd={}):
-
'''Return a dictionary of node names and attribute names and values
-
found in an XML document parseString instance. The keys are the node
-
names and each value is a list of dictionaries (a list is used since an
-
XML document can have multiple nodes with the same name). The keys of
-
each dictionary in the list are the attribute names and the values are
-
the attribute values. All keys and values are converted to regular
-
strings.'''
-
if doc == None: return None
-
for child in doc.childNodes:
-
# Skip all nodes except ELEMENT_NODE
-
if child.nodeType == 1:
-
# dict of attribute/values
-
s = child.attributes
-
if s:
-
dd.setdefault(str(child.nodeName),
-
[]).append(dict(zip([str(item) for item in s.keys()],
-
[str(item.value) for item in s.values()])))
-
if child.hasChildNodes():
-
dd = self.getNodeDict(child, dd)
-
return dd
-
-
print
-
-
for key, value in getNodeDict(xmlDoc).items():
-
print "Node Name: %s" % (key)
-
print "Attributes:"
-
for item in value:
-
for x in item:
-
print ' %s: %s' % (x, item[x])
Output: - >>>
-
Node Name: amman
-
Attributes:
-
PrevUsed: project1
-
Name: third
-
Hazard: weapons
-
PhotoAddress: C:\Users arik\Desktop\DSC_0018.jpg
-
Phone: 6453222
-
Address: 3rd area
-
Owner: tarik
-
>>>
This code has not been tested on anything other than your document.
| | Member | | Join Date: Jun 2007
Posts: 66
| | | re: simple XML question
i cant thank you enough!!! i bow before you master,
i shall try the code tomorrow i hope cause it's darn late here,
thanks again
T
| | Member | | Join Date: Jun 2007
Posts: 66
| | | re: simple XML question
couldnt sleep without trying it!
first of all, it works perfectly, i dont know how to thank you,
but the boring part is, can i ask a couple of questions concerning?
if child.nodeType == 1: # very clear, but how do i get a reference of what this might return? i've read XML documents and ive read MINIDOM documents, but the gap in between of what to use where, how do you recommend i understand that?
dd.setdefault(str(child.nodeName),[]).append(dict(zip([str(item) for item in s.keys()],
[str(item.value) for item in s.values()])))
#i get how it's working, but this is an out of place question, how do i understand functions like "setdefault" and "zip" ? i mean the documantation can only get u this much, then it ends up losing u! i recently moved from Autodesk|Maya's python, and working on programming more OS stuff,
thank you so much for your help!
T
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,563
| | | re: simple XML question
To begin with, I like this website for learning the basics of XML.
There are several types of nodes, one type being ELEMENT_NODE. - >>> xmlDoc.ELEMENT_NODE
-
1
-
>>> xmlDoc.firstChild.nodeType
-
1
To see a list of the available attributes of an XML document parseString instance: - >>> for item in dir(xmlDoc):
-
... print item
-
...
-
ATTRIBUTE_NODE
-
CDATA_SECTION_NODE
-
COMMENT_NODE
-
DOCUMENT_FRAGMENT_NODE
-
DOCUMENT_NODE................................
Built-in function dict() returns a dictionary. Built-in function zip() returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences. Example: - >>> s1 = [1,2,3]
-
>>> s2 = ['a','b','c']
-
>>> zip(s1, s2)
-
[(1, 'a'), (2, 'b'), (3, 'c')]
-
>>> dict(zip(s1,s2))
-
{1: 'a', 2: 'b', 3: 'c'}
-
>>>
Dictionary method setdefault(key[, x]) returns the value of the dictionary key if the key exists, otherwise returns x and sets the dictionary key to x. Example: - >>> dd = dict(zip(s1,s2))
-
>>> dd.setdefault(3, [])
-
'c'
-
>>> dd.setdefault(4, [])
-
[]
-
>>> dd
-
{1: 'a', 2: 'b', 3: 'c', 4: []}
-
>>> dd.setdefault(4, []).append('d')
-
>>> dd
-
{1: 'a', 2: 'b', 3: 'c', 4: ['d']}
-
>>>
| | Member | | Join Date: Jun 2007
Posts: 66
| | | re: simple XML question
could not have been clearer! thank you master
cheers
T
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|