472,131 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

parse xml like a 2 dimentional array

i have an xml document that i need to parse in the most efficient way, the syntax of the document is like this
Expand|Select|Wrap|Line Numbers
  1.  <?xml version="1.0" ?> 
  2. - <frameNumber4>
  3. - <v0>
  4.   <X>0.148778095841</X> 
  5.   <Y>-0.987688362598</Y> 
  6.   <Z>-0.0483409352601</Z> 
  7.   </v0>
  8. - <v1>
  9.   <X>0.126558214426</X> 
  10.   <Y>-0.987688362598</Y> 
  11.   <Z>-0.0919499248266</Z> 
  12.   </v1>
  13. etc....

so i need to parse this as in im accessing <v0><X>, is this possible?

i know i can do it in a manner where i take the v0 and parse it again to get the X, but i was wondering if there was a more efficient way of direct access to a 2nd level member

on the other hand, i know this should be a really dumb question, but how do i get the value of the element, and not the whole xml, all i know of is the method toxml which gives me the whole <Z>-0.0919499248266</Z>

and here's the code im using so far in case it should come in handy :)

Expand|Select|Wrap|Line Numbers
  1. from xml.dom.minidom import parse
  2. dom = parse('D:\\newCache\\pSphere2-004.xml')
  3. for node in dom.getElementsByTagName('v0'): 
  4.     print node.toxml()
  5.  
  6.  
Jan 2 '08 #1
3 1976
bvdet
2,851 Expert Mod 2GB
Following are a couple of ways to get the data in a usable form:
Expand|Select|Wrap|Line Numbers
  1. from xml.dom import minidom
  2.  
  3. doc = minidom.parse('your.xml')
  4.  
  5. def get_XYZ(elem):
  6.     x = float(elem.getElementsByTagName("X")[0].firstChild.data)
  7.     y = float(elem.getElementsByTagName("Y")[0].firstChild.data)
  8.     z = float(elem.getElementsByTagName("Z")[0].firstChild.data)
  9.     return x,y,z    
  10.  
  11. # Get document element matching a given tag name
  12. print get_XYZ(doc.getElementsByTagName("v0")[0])
  13. print get_XYZ(doc.getElementsByTagName("v1")[0])
  14.  
  15. def get_XYZ1(name):
  16.     output = []
  17.     for node in doc.getElementsByTagName(name):
  18.         t = node.firstChild.nextSibling
  19.         while t:
  20.             s = repr(t).strip()
  21.             if not '\\n' in s:
  22.                 output.append([name, str(t.localName), float(t.firstChild.data)])
  23.             t = t.nextSibling
  24.     return output
  25.  
  26. print get_XYZ1('v0')
  27. print get_XYZ1('v1')
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> (0.14877809584099999, -0.98768836259799997, -0.048340935260099999)
  2. (0.12655821442599999, -0.98768836259799997, -0.091949924826599999)
  3. [['v0', 'X', 0.14877809584099999], ['v0', 'Y', -0.98768836259799997], ['v0', 'Z', -0.048340935260099999]]
  4. [['v1', 'X', 0.12655821442599999], ['v1', 'Y', -0.98768836259799997], ['v1', 'Z', -0.091949924826599999]]
  5. >>> 
Jan 3 '08 #2
seriously thank you! i cant thank you enough,

but if i may ask one more thing, i really looked around the internet for good documentation, noone even mentioned the ".data", what source do you recommend??
Jan 3 '08 #3
bvdet
2,851 Expert Mod 2GB
seriously thank you! i cant thank you enough,

but if i may ask one more thing, i really looked around the internet for good documentation, no one even mentioned the ".data", what source do you recommend??
I have found a few simple parsing examples on the internet, and I use Python Essential Reference and Python documentation for reference, which are both very basic. The data attribute is only available for text nodes. I am trying to learn XML parsing also. It helps to know XML. There are many online resources on XML, DOM and SAX.
Jan 3 '08 #4

Post your reply

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

Similar topics

2 posts views Thread by Cedric Baelemans | last post: by
1 post views Thread by rkmoray | last post: by
9 posts views Thread by Srinu | last post: by

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.