test_gml.py
from lxml import etree
import lxml
xml_response = lxml.etree.Element('xmlResponse')
mapping = lxml.etree.SubElement(xml_response,'mapping',{"id":"AHGJHKSHQWEIUELQWDKJQEDH"})
services_element = lxml.etree.SubElement(mapping, 'service_name')
services_element.text = "gml_info"
gml_info = '''<gml:Envelope srsName="EPSG:4326">
<gml:lowerCorner>1 2</gml:lowerCorner>
<gml:upperCorner>10 20</gml:upperCorner>
</gml:Envelope>'''
services_element = lxml.etree.SubElement(mapping, 'gml_info')
services_element.text = gml_info
print (etree.tostring(xml_response))
当我尝试运行上述代码时,我得到以下响应,其中未保留 gml 标签。
回复:
b'<xmlResponse><mapping id="AHGJHKSHQWEIUELQWDKJQEDH">
<service_name>gml_info</service_name>
<gml_info><gml:Envelope
srsName="EPSG:4326">\n\t\t<gml:lowerCorner>1
2</gml:lowerCorner>\n\t\t<gml:upperCorner>10
20</gml:upperCorner>\n\t</gml:Envelope></gml_info>
</mapping></xmlResponse>'