无法使用 python 中的 lxml 将 gml 字符串附加到 xml
Unable to append the gml string to xml using lxml in python
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>'
您必须将其添加为 xml
子树而不是 text node
。
from lxml import etree
import lxml
import io
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 = io.StringIO('''<root xmlns:gml="gml"><gml:Envelope srsName="EPSG:4326">\
<gml:lowerCorner>1 2</gml:lowerCorner>\
<gml:upperCorner>10 20</gml:upperCorner>\
</gml:Envelope></root>''')
gml_info = etree.parse(gml).getroot()
services_element = lxml.etree.SubElement(mapping, 'gml_info')
services_element.extend(gml_info)
print(etree.tostring(xml_response))
我不得不为 gml
添加带有名称空间声明的 <root xmlns:gml="gml">
元素(您应该将其更改为真正的 link)。没有它你会得到错误:
lxml.etree.XMLSyntaxError: Namespace prefix gml on Envelope is not defined, line 1, column 41
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>'
您必须将其添加为 xml
子树而不是 text node
。
from lxml import etree
import lxml
import io
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 = io.StringIO('''<root xmlns:gml="gml"><gml:Envelope srsName="EPSG:4326">\
<gml:lowerCorner>1 2</gml:lowerCorner>\
<gml:upperCorner>10 20</gml:upperCorner>\
</gml:Envelope></root>''')
gml_info = etree.parse(gml).getroot()
services_element = lxml.etree.SubElement(mapping, 'gml_info')
services_element.extend(gml_info)
print(etree.tostring(xml_response))
我不得不为 gml
添加带有名称空间声明的 <root xmlns:gml="gml">
元素(您应该将其更改为真正的 link)。没有它你会得到错误:
lxml.etree.XMLSyntaxError: Namespace prefix gml on Envelope is not defined, line 1, column 41