Top | Previous | Next |
Parsing XML Results |
It's quite easy deal with return values of method calls when they are simple none structured values like floats or integers. However it's not always the case that you will have simple single values returned from a method call. Many times web services will return values that have some sort of structure to them like a dataset. Since there is no way for the web service to know how this value should be represented in the language/environment that called the method it is common that the result will be returned in an XML formatted string. Dealing with these strings is not really part of the SUDS library but an example of XML string handling is included here for completeness. This example makes use of the xml.dom.minidom python module to parse the XML string and pull out the data.
Periodic Table Example - Using xml.dom.minidom XML parsing One of the public web services that are available has a method for returning the elements in the periodic table in a structured string. This example will show you how you can take that string result, parse through it and then create an Ignition dataset that can be displayed in a table component. The xml.dom.minidom library provides the functionality for parsing XML strings and returning what is called an XML Document (in fact DOM stands for Document Object Model). This Document has functions that allow you access the elements within the document by specifying their respective XML tags. Most of this library is out of the scope of this document but if you have any questions about the functions being used they can be found here and here.
from suds.client import Client from xml.dom.minidom import parseString client = Client("http://www.webservicex.net/periodictable.asmx?WSDL") elements = client.service.GetAtoms()
<NewDataSet> <Table> <ElementName>Actinium</ElementName> </Table> ... </NewDataSet>
dom = parseString(xml_string) xmlTags = dom.getElementsByTagName('ElementName') xmlData = [] for tag in xmlTags: xmlData.append(tag.firstChild.data)
element_weights = [] element_numbers = [] element_symbols = [] for name in element_names: weight = (parseString(client.service.GetAtomicWeight(name)). getElementsByTagName('AtomicWeight')[0].firstChild.data) number = (parseString(client.service.GetAtomicNumber(name)). getElementsByTagName('AtomicNumber')[0].firstChild.data) symbol = (parseString(client.service.GetElementSymbol(name)). getElementsByTagName('Symbol')[0].firstChild.data) element_weights.append(weight) element_numbers.append(number) element_symbols.append(symbol)
headers = ["Name", "Symbol", "Weight", "Number"] i = 0 rows = [] for name in element_names: oneRow = [name,element_symbols[i],element_weights[i],element_numbers[i]] rows.append(oneRow) i += 1 data = system.dataset.toDataSet(headers, rows) event.source.parent.getComponent('Table').data = data
|