inc*_*to2 15 python doctype lxml xml-declaration
我正在使用python的lxml,我正在尝试读取xml文档,修改并将其写回,但原始的doctype和xml声明消失了.我想知道是否有一种简单的方法可以通过lxml或其他解决方案将其放回去?
Joh*_*yes 12
以下将包括DOCTYPE和XML声明:
# adds declaration with version and encoding regardless of
# which attributes were present in the original declaration
# expects utf-8 encoding (encode/decode calls)
# depending on your needs you might want to improve that
from lxml import etree
from xml.dom.minidom import parseString
xml1 = '''\
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root SYSTEM "example.dtd">
<root>...</root>
'''
xml2 = '''\
<root>...</root>
'''
def has_xml_declaration(xml):
return parseString(xml).version
def process(xml):
t = etree.fromstring(xml.encode()).getroottree()
if has_xml_declaration(xml):
print(etree.tostring(t, xml_declaration=True, encoding=t.docinfo.encoding).decode())
else:
print(etree.tostring(t).decode())
process(xml1)
process(xml2)
Run Code Online (Sandbox Code Playgroud)
注意,如果您创建(例如使用),tostring则不保留它,它仅在您使用处理XML时起作用.DOCTYPEElementfromstringparse
更新:正如JF Sebastian所指出的,我的断言fromstring是不正确的.
下面是一些代码来突出之间的差异Element和ElementTree系列化:
from lxml import etree
from StringIO import StringIO
tree = etree.parse(StringIO('''<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE root SYSTEM "test" [ <!ENTITY tasty "eggs"> ]>
<root>
<a>&tasty;</a>
</root>
'''))
docinfo = tree.docinfo
print etree.tostring(tree, xml_declaration=True, encoding=docinfo.encoding)
Run Code Online (Sandbox Code Playgroud)
输出是:
from lxml import etree
from StringIO import StringIO
xml_str = '''<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE root SYSTEM "test" [ <!ENTITY tasty "eggs"> ]>
<root>
<a>&tasty;</a>
</root>
'''
# get the ElementTree using parse
parse_tree = etree.parse(StringIO(xml_str))
encoding = parse_tree.docinfo.encoding
result = etree.tostring(parse_tree, xml_declaration=True, encoding=encoding)
print "%s\nparse ElementTree:\n%s\n" % ('-'*20, result)
# get the ElementTree using fromstring
fromstring_tree = etree.fromstring(xml_str).getroottree()
encoding = fromstring_tree.docinfo.encoding
result = etree.tostring(fromstring_tree, xml_declaration=True, encoding=encoding)
print "%s\nfromstring ElementTree:\n%s\n" % ('-'*20, result)
# DOCTYPE is lost, and no access to encoding
fromstring_element = etree.fromstring(xml_str)
result = etree.tostring(fromstring_element, xml_declaration=True)
print "%s\nfromstring Element:\n%s\n" % ('-'*20, result)
Run Code Online (Sandbox Code Playgroud)
您还可以使用以下内容保留DOCTYPE和XML声明fromstring():
import sys
from StringIO import StringIO
from lxml import etree
xml = r'''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>example</title>
</head>
<body>
<p>This is an example</p>
</body>
</html>'''
tree = etree.fromstring(xml).getroottree() # or etree.parse(file)
tree.write(sys.stdout, xml_declaration=True, encoding=tree.docinfo.encoding)
Run Code Online (Sandbox Code Playgroud)
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>example</title>
</head>
<body>
<p>This is an example</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
请注意,xml声明(具有正确的编码)和doctype存在.它甚至(可能不正确)使用'而不是"在xml声明中并添加Content-Type到<head>.
对于@John Keyes的示例输入,它会产生与etree.tostring()答案中相同的结果.
| 归档时间: |
|
| 查看次数: |
5859 次 |
| 最近记录: |