-2

I'm looking for way to get entire element from xml file using python 2.7, based on line number where error occurs.

http://pastebin.com/GjmPwg1a here is an example of my xml file (in full version there is millions of lines).

I need to get everything between <featureMember> tag and have no brightest idea how to achive that, so any help would be appreciated. Thanks a lot.

1 Answers1

0

There is different ways for XML parsing in Python. For example, without installing lxml you can use minidom.

Sample code is:

from urllib.request import urlopen
from xml.dom import minidom

# your address with raw xml output
address = "http://pastebin.com/raw/GjmPwg1a"

webPage = urlopen(address)
response = str(webPage.read(), encoding='utf-8')

tree = minidom.parseString(response)
nodes = tree.getElementsByTagName('gml:featureMember')

for node in nodes:
    print(node.toxml())

Just add your nodes processing in for, for example.

And sure, attached your code first will be helpful.

Related docs: http://www.diveintopython.net/xml_processing/parsing_xml.html

Similar exists topics: just search "stackoverflow parsing xml python" in Google.

mrDinkelman
  • 488
  • 1
  • 9
  • 18