473,799 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parse xml like a 2 dimentional array

75 New Member
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 2028
bvdet
2,851 Recognized Expert Moderator Specialist
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
askalottaqs
75 New Member
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 Recognized Expert Moderator Specialist
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
2228
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? Is there something special I have to take into account? Any help is welcome Cedric
8
5137
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' should be a constant, but I want it to be defined from the user, so what should I do? and how must 'a' be declared?? help me please
1
3247
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; foreach(DataRow myDataRow in myDataTable.Rows) {
0
2466
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 dimentional array. System::String* tda = new System::String*; tda = "lastName"; tda = "Power"; tda = "firstName"; tda = "Max";
14
2360
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 pass the array as argument with no other info like number of strings. Is there a way to achieve that?
2
2418
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 cheese. It has four different difficulty levels and you have obsticles in your way (the amount depends on the difficulty level). The problem I am having is that when I move the 'M' (mousebot) into the array space that the cheese ('C') is in it...
3
2446
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) this is my declared array of byte size (6)
9
2467
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 the operand to this operator. i.e *p is the value at address given by p. 3. In case of two dimentional array a, a+1 refers to the second
3
2429
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 arrayName = decimal value say 3 arrayName = name say xyz
0
9543
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.