# Parse a document and print out the entire parse tree

 

# Parse a document and print out the entire parse tree

 

from xml.dom import minidom

 

doc = minidom.parse(r'H:\TEMP\temsys\sampleXML1.xml')

 

def print_tree(n, indent=0):

    while n:

        s = repr(n).strip()

        if not '\\n' in s:

            print ' '*indent, s

        print_tree(n.firstChild,indent+4)

        n = n.nextSibling

'''

print_tree(doc)

print 'Done'

'''

def get_XYZ(elem):

    x = float(elem.getElementsByTagName("X")[0].firstChild.data)

    y = float(elem.getElementsByTagName("Y")[0].firstChild.data)

    z = float(elem.getElementsByTagName("Z")[0].firstChild.data)

    return x,y,z   

 

# Get all document elements matching a given tag name

print get_XYZ(doc.getElementsByTagName("v0")[0])

print get_XYZ(doc.getElementsByTagName("v1")[0])

 

def get_XYZ1(name):

    output = []

    for node in doc.getElementsByTagName(name):

        t = node.firstChild.nextSibling

        while t:

            s = repr(t).strip()

            if not '\\n' in s:

                output.append([name, str(t.localName), float(t.firstChild.data)])

            t = t.nextSibling

    return output

 

print get_XYZ1('v0')

print get_XYZ1('v1')

 

#print '%s: %s = %0.6f' % (name, t.localName, float(t.firstChild.data))

 

'''

for node in doc.getElementsByTagName('v0'):

    print node.toxml()

'''   

 

 

'''

# Loop over all items extracting an attribute and text data

for i in items:

    quantity = i.getAttribute('X')

    text = ""

    t = i.firstChild

    # Collect data from immediate children

    while t:

        if t.nodeType == t.TEXT_NODE:

            text += t.data

        t = t.nextSibling

    print quantity, text

'''

 

''' Output

>>> (0.14877809584099999, -0.98768836259799997, -0.048340935260099999)

(0.12655821442599999, -0.98768836259799997, -0.091949924826599999)

[['v0', 'X', 0.14877809584099999], ['v0', 'Y', -0.98768836259799997], ['v0', 'Z', -0.048340935260099999]]

[['v1', 'X', 0.12655821442599999], ['v1', 'Y', -0.98768836259799997], ['v1', 'Z', -0.091949924826599999]]

>>>

'''

 

''' XML data

<?xml version="1.0" ?>

 <frameNumber4>

 <v0>

  <X>0.148778095841</X>

  <Y>-0.987688362598</Y>

  <Z>-0.0483409352601</Z>

 </v0>

 <v1>

  <X>0.126558214426</X>

  <Y>-0.987688362598</Y>

  <Z>-0.0919499248266</Z>

 </v1>

 </frameNumber4>

'''