473,699 Members | 2,202 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()
Jan 2 '08 #1
3 2024
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
  3. doc = minidom.parse('your.xml')
  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    
  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])
  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
  26. print get_XYZ1('v0')
  27. print get_XYZ1('v1')
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
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
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

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
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
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) {
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";
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?
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...
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)
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
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
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,...
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,...
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...
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...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
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.