Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python read xml

from xml.dom import minidom

# parse an xml file by name
mydoc = minidom.parse('items.xml')

items = mydoc.getElementsByTagName('item')

# one specific item attribute
print('Item #2 attribute:')
print(items[1].attributes['name'].value)

# all item attributes
print('
All attributes:')
for elem in items:
    print(elem.attributes['name'].value)

# one specific item's data
print('
Item #2 data:')
print(items[1].firstChild.data)
print(items[1].childNodes[0].data)

# all items data
print('
All item data:')
for elem in items:
    print(elem.firstChild.data)
Comment

read xml file in python

import  xml.dom.minidom
from xml.dom import getChildNodesByName


xdom = xml.dom.minidom.parse("GenericAddressing.xml")
xdoc = xdom.documentElement
dict = {}
xml_funcs = getChildNodesByName(xdoc, u"Function")
for func in xml_funcs:
    shortname = func.getAttribute(u"Name")
    address = func.getAttribute(u"Address")
    for name in  getChildNodesByName(func, u"Name"):
        longname = name.firstChild.nodeValue
        dict[hex(int(address))[2:].upper()] = (shortname, longname)
        break
Comment

PREVIOUS NEXT
Code Example
Python :: import matplotlib pyplot as plt 
Python :: python if not 
Python :: casting in python 
Python :: remove trailing zeros python 
Python :: dict to string 
Python :: catch exception python unittest 
Python :: pd.concat in python 
Python :: if equal to key return value python 
Python :: django queryset multiple filters 
Python :: python delete list elements 
Python :: python dict if key does not exist 
Python :: python if boolean logic 
Python :: change time format pm am in python 
Python :: python decision tree 
Python :: set default formatter for python vscode 
Python :: python create a program that runs through all possible combinations 
Python :: selenium python tkinter 
Python :: how to make a list in python 
Python :: transpose of a matrix in python numpy 
Python :: Is python statically typed language? 
Python :: pandas list comprehension 
Python :: create 2d array with rows and columns 
Python :: python type checking 
Python :: ipython and virtualenvs 
Python :: python regex validate phone number 
Python :: slicing strings in python 
Python :: character in python 
Python :: TfidfVectorizer use 
Python :: how to change order of attributes of an element using beautiful soup 
Python :: python replace string with int in list 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =