I understand xml and minidom but I am missing something. What I want to do is be able to parse a variable size XML file and be able to build a tree of the elements.
Take this example
<doc>
<foo>
<server>
<ip>10.1.1.1</ip>
<service>service1</service>
<name>somename</name>
<something>sometext</something>
</server>
</foo>
<foo>
<server>
<ip>10.1.1.2</ip>
<service>service2</service>
<name>other</name>
<something>link</something>
</server>
</foo>
<foo>
<server>
<ip>10.1.1.3</ip>
<service>service2</service>
<name>other2</name>
<something>link</something>
</server>
</foo>
</doc>
Now I want to be able to parse the tree and get association so for instance service belongs to some server. And IP belongs to this one. I want to keep the tree (with server being the key) and have all the elements stored. Than I also want to be able to search for services in all the servers or display all IPs..etc..etc..
I think I will need to build a dictionary but I don't know how I can build a nested dictionary where I have everything be based of a single key.
Here's my quick code. I have a list of elements implementation and a list of servers implementation(+get_dict()). This should be all you need, as based on the question, afaik.
import lxml.etree as etree
class server(object):
def __init__(self):
self.ip = ''
self.service = ''
self.name = ''
self.something = ''
def get_dict(self):
return {self:{'ip':self.ip,'service':self.service,'name':self.name,'something':self.something}}
class serverlist(object):
def __init__(self):
self.servers = []
def addserver(self,serverobject):
if serverobject not in self.servers:
self.servers.append(serverobject)
def removeserver(self,serverobject):
for s in self.servers:
if s.get_dict() == serverobject.get_dict():
self.servers.remove(s)
def findserver(self,valueid,value):
for s in self.servers:
if s.get_dict()[s][valueid] == value:
return s
return None
xml = etree.fromstring(r'''<xml><foo>
<server>
<ip>10.1.1.1</ip>
<service>service1</service>
<name>somename</name>
<something>sometext</something>
</server>
</foo>
<foo>
<server>
<ip>10.1.1.2</ip>
<service>service2</service>
<name>other</name>
<something>link</something>
</server>
</foo>
<foo>
<server>
<ip>10.1.1.3</ip>
<service>service2</service>
<name>other2</name>
<something>link</something>
</server>
</foo></xml>''')
#servers as a list of items (class server)
servers1 = []
for s in list(xml):
tempserver = server()
tempserver.ip=s.find('.//ip').text
tempserver.service=s.find('.//service').text
tempserver.name = s.find('.//name').text
tempserver.something = s.find('.//something').text
servers1.append(tempserver)
print servers1
#servers as a list of elements
servers2 = xml.xpath('.//server')
print servers2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.