473,396 Members | 1,996 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,396 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 2013
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

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

Similar topics

2
by: Cedric Baelemans | last post by:
Hi, I try to store a two-dimentional array in a session variable. When I do the same with a one-dimentional array, it works fine, but with a two-dimentional array it fails. Is this not possible?...
8
by: Al | last post by:
I'd like to declare (in a Matrix class) a two-dimentional array with user-defined coordinates. The constructor is: Matrix(int c, int l): col(c), lin(l), a(new float) {} the compiler says 'lin'...
1
by: rkmoray | last post by:
I have created a Multi Dimentional array, but having problems filling it. int mCount=0; mCount=ds.Tables.Rows.Count; string arrayppsa = new string ; DataTable myDataTable=ds.Tables;...
0
by: Max M. Power | last post by:
I'm getting an InvalidOperationException calling Invoke with a method parameter object array that contains a two dimentional string array. More stack trace output below: // Create two...
14
by: ranjmis | last post by:
Hi all, Below is the code wherein I am initializing double dimentional array inside main with string literals. Now I want to display the strings using a function call to which I just want to...
2
by: Icarus27 | last post by:
Hello, I am making a program that runs off of a two dimentional array but I have made it into a game called MouseBot. I am trying to make the game so that you can move the mouse until you get the...
3
by: cmdolcet69 | last post by:
I can the following error message "value of type byte cannot be converted to 1-dimentional array of byte" when i execute this line of code: moRS232.Write(Packet(i)) it underline the packet(i)...
9
by: Srinu | last post by:
Hi All, We know the following facts, 1. A two dimentional arrays in C is stored similar to a single dimentional array. 2. * is the dereference operator that gives value at address denoted by...
3
by: neovantage | last post by:
Hey all, I need to sort 2 dimentional array. My data is in this form arrayName = decimal value say 1 arrayName = name say abc arrayName = decimal value say 2 arrayName = name say def...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.