XMLWriter#
- class astropy.utils.xml.writer.XMLWriter(file)[源代码]#
基类:
object
一个编写格式良好且缩进良好的XML的类。
这样使用:
w = XMLWriter(fh) with w.tag('html'): with w.tag('body'): w.data('This is the content')
生产:
<html> <body> This is the content </body> </html>
- 参数:
- file : file-like (writeable)类似文件(可写)
方法总结
close
\(ID)关闭打开的元素,直到(包括)由给定标识符标识的元素。
comment
\(评论)向输出流添加注释。
data
\(文本)将字符数据添加到输出流。
element
(tag[, text, wrap, attrib])添加整个元素。
end
([tag, indent, wrap])关闭当前元素(通过最近对
start
)flush
\()get_indentation
\()返回文件当前的缩进级别数。
get_indentation_spaces
([offset])返回与当前缩进级别匹配的空格字符串。
object_attrs
(obj, attrs)将对象上具有一组属性的对象转换为字典以供
XMLWriter
.start
(tag[, attrib])打开新元素。
string_element
(xml_string)重新格式化XML中的缩进以插入MIVOT块。
tag
(tag[, attrib])使用
with
语句。xml_cleaning_method
([method])上下文管理器控制如何清除(转义)XML数据标记以删除潜在的不安全字符或构造。
方法文件
- end(tag=None, indent=True, wrap=False)[源代码]#
关闭当前元素(通过最近对
start
)- 参数:
- tag :
str
Python :字符串 元素名称。如果给定,则标记必须与起始标记匹配。如果省略,则当前元素将关闭。
- tag :
- start(tag, attrib={}, **extra)[源代码]#
打开新元素。属性可以作为关键字参数或字符串/字符串字典给定。方法返回可传递给
close()
方法,关闭此元素之前(包括该元素)的所有打开元素。
- tag(tag, attrib={}, **extra)[源代码]#
使用
with
语句。实例
>>> with writer.tag('foo'): ... writer.element('bar') ... # </foo> is implicitly closed here ...
参数与
start
.
- xml_cleaning_method(method='escape_xml', **clean_kwargs)[源代码]#
上下文管理器控制如何清除(转义)XML数据标记以删除潜在的不安全字符或构造。
默认值 (
method='escape_xml'
)对某些关键XML字符应用暴力转义,如<
,>
和&
以确保输出不是有效的XML。为了显式地允许某些XML标记(例如,链接引用或强调标记),使用
method='bleach_clean'
。这将使用clean
的功能 bleach 包裹。任何其他关键字参数都将直接传递给clean
功能。最后,使用
method='none'
禁用任何卫生处理。这应该谨慎使用。例子::
w = writer.XMLWriter(ListWriter(lines)) with w.xml_cleaning_method('bleach_clean'): w.start('td') w.data('<a href="https://google.com">google.com</a>') w.end()