Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the ability to output CDATA wrapped strings #34

Open
GoogleCodeExporter opened this issue Feb 11, 2016 · 0 comments
Open

Improve the ability to output CDATA wrapped strings #34

GoogleCodeExporter opened this issue Feb 11, 2016 · 0 comments

Comments

@GoogleCodeExporter
Copy link

Example script from Sean:

#!/usr/bin/python
from lxml import etree
from pykml.factory import KML_ElementMaker as KML

TEXT_ELEMENTS = ['description', 'text', 'linkDescription', 'displayName']

def getXmlWithCDATA(obj, cdata_elements):
  # Convert Objectify document to lxml.etree (is there a better way?)
  root = etree.fromstring(etree.tostring(etree.ElementTree(obj)))

  #Create an xpath expression to search for all desired cdata elements
  xpath = '|'.join(map(lambda tag: '//kml:' + tag, cdata_elements))

  results = root.xpath(xpath, namespaces = {'kml': 'http://www.opengis.net/kml/2.2'})
  for element in results:
    element.text = etree.CDATA(element.text)

  return root

kmlobj = KML.kml(
    KML.Document(
        KML.Placemark(
            KML.name('foobar'),
            KML.styleUrl('#big_label'),
            KML.description('<html>'),
            KML.text('<html>'),
            KML.linkDescription('<html>'),
            KML.displayName('<html>')
        )
    )
)

kmlobj_with_cdata = getXmlWithCDATA(kmlobj, TEXT_ELEMENTS)
print etree.tostring(kmlobj_with_cdata, pretty_print=True)

Original issue reported on code.google.com by [email protected] on 22 Feb 2012 at 12:56

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant