-2

I have details like this on an XML file

<note>
<id>51</id>
<Name>Jani</Name>
<city>Frankfurt</city>
<IQ>Intelligent</IQ>
</note>

<note>
<id>71</id>
<Name>Peter</Name>
<city>Paris</city>
<IQ>Average</IQ>
</note>

<note>
<id>81</id>
<Name>Asho</Name>
<city>Paris</city>
<IQ>Intelligent</IQ>
</note>

Given these details, I want to implement a search engine that should enable the user to search for all the 'Intelligent' people in the file.

Kindly suggest me the best way to do it in python.

viggie
  • 183
  • 1
  • 3
  • 11

2 Answers2

1

Use lxml and XPath:

from StringIO import StringIO
from lxml import etree

xml = """<root>
<note>
<id>51</id>
<Name>Jani</Name>
<city>Frankfurt</city>
<IQ>Intelligent</IQ>
</note>

<note>
<id>71</id>
<Name>Peter</Name>
<city>Paris</city>
<IQ>Average</IQ>
</note>

<note>
<id>81</id>
<Name>Asho</Name>
<city>Paris</city>
<IQ>Intelligent</IQ>
</note>

</root>"""

tree = etree.parse(StringIO.StringIO(xml))
for note in tree.xpath("//note[IQ='Intelligent']"):
    print note.getchildren()[1].tag + "=" + note.getchildren()[1].text

This prints:

Name=Jani
Name=Asho
  • I want the search engine to get all the information of all the 'Intelligent' people, so that I can access it to use for some other purpose. – viggie Jun 14 '13 at 09:42
  • You can do that. Just access the information of each `note`. See the lxml documentation for details. –  Jun 14 '13 at 09:44
0

if the file is small them the libary xml.mindom can give you an easy to transvert data structure if the file is larger I would suggest xml.etree.cElementTree

alternativly you could preparse your file as needed, for example: make a dict (or list if all numeric ids are used) that has tuples/list with relevant data then make a name, city and IQ dict that contains lists with ids such that:

note[51] = (51,'Jani', 'Frankfurt', 'Intelligent')

and

iq_dict['Intelligent'] = (81, 51)

etc..

jcr
  • 1,015
  • 6
  • 18