tylere / pykml

pyKML is a Python package for creating, parsing, manipulating, and validating KML documents.
BSD 3-Clause "New" or "Revised" License
13 stars 9 forks source link

Error localization when kml does not match kml. #15

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Use some xml tool to better localize problems with a document not matching a 
schema.  

Ugly solution... call xmllint command line program.  Yuck

Better would be to figure out how to call something from lxml or libxml2.  
Would be best to use lxml if at all possible to keep the dependencies down.

Additionally, is it possible to validate fragments of kml?

Original issue reported on code.google.com by schw...@gmail.com on 3 Aug 2011 at 9:09

GoogleCodeExporter commented 8 years ago
Just found this from the lxml mailing list where the person had trouble getting 
the line number if it was over 65k... so trouble for big KML's, but it is a 
start.

http://permalink.gmane.org/gmane.comp.python.lxml.devel/6085

def validate_xml(xml_filename, xsd_filename):

    # real dom config xsd
    xsd_fd = open(xsd_filename, 'r')
    xmlschema_doc = etree.parse(xsd_fd)
    xsd = etree.XMLSchema(xmlschema_doc)

    doc = open(xml_filename, 'r').read()
    doc_xml = etree.XML(doc)

    if xsd.validate(doc_xml):
        print "valid"
    else:
        print "invalid"
        print xsd.error_log

Original comment by schw...@gmail.com on 4 Aug 2011 at 3:56