MCPcopy Create free account
hub / github.com/STIXProject/python-stix / to_xml

Method to_xml

stix/base.py:94–180  ·  view source on GitHub ↗

Serializes a :class:`Entity` instance to an XML string. The default character encoding is ``utf-8`` and can be set via the `encoding` parameter. If `encoding` is ``None``, a string (unicode in Python 2, str in Python 3) is returned. Args: auto_namespace:

(self, include_namespaces=True, include_schemalocs=False,
               ns_dict=None, schemaloc_dict=None, pretty=True,
               auto_namespace=True, encoding='utf-8')

Source from the content-addressed store, hash-verified

92 self._set_var(klass, **kwargs)
93
94 def to_xml(self, include_namespaces=True, include_schemalocs=False,
95 ns_dict=None, schemaloc_dict=None, pretty=True,
96 auto_namespace=True, encoding='utf-8'):
97 """Serializes a :class:`Entity` instance to an XML string.
98
99 The default character encoding is ``utf-8`` and can be set via the
100 `encoding` parameter. If `encoding` is ``None``, a string (unicode in
101 Python 2, str in Python 3) is returned.
102
103 Args:
104 auto_namespace: Automatically discover and export XML namespaces
105 for a STIX :class:`Entity` instance.
106 include_namespaces: Export namespace definitions in the output
107 XML. Default is ``True``.
108 include_schemalocs: Export ``xsi:schemaLocation`` attribute
109 in the output document. This will attempt to associate
110 namespaces declared in the STIX document with schema locations.
111 If a namespace cannot be resolved to a schemaLocation, a
112 Python warning will be raised. Schemalocations will only be
113 exported if `include_namespaces` is also ``True``.
114 ns_dict: Dictionary of XML definitions (namespace is key, alias is
115 value) to include in the exported document. This must be
116 passed in if `auto_namespace` is ``False``.
117 schemaloc_dict: Dictionary of XML ``namespace: schema location``
118 mappings to include in the exported document. These will
119 only be included if `auto_namespace` is ``False``.
120 pretty: Pretty-print the XML.
121 encoding: The output character encoding. Default is ``utf-8``. If
122 `encoding` is set to ``None``, a string (unicode in Python 2,
123 str in Python 3) is returned.
124
125 Returns:
126 An XML string for this
127 :class:`Entity` instance. Default character encoding is ``utf-8``.
128
129 """
130
131 from mixbox.entities import NamespaceCollector
132
133 if (not auto_namespace) and (not ns_dict):
134 raise Exception(
135 "Auto-namespacing was disabled but ns_dict was empty "
136 "or missing."
137 )
138
139 ns_info = NamespaceCollector()
140
141 obj = self.to_obj(ns_info=ns_info if auto_namespace else None)
142
143 ns_info.finalize(ns_dict=ns_dict, schemaloc_dict=schemaloc_dict)
144
145 if auto_namespace:
146 obj_ns_dict = ns_info.binding_namespaces
147 else:
148 obj_ns_dict = dict(
149 itertools.chain(
150 iteritems(ns_info.binding_namespaces),
151 iteritems(namespaces.get_full_ns_map())

Callers 15

to_xmlMethod · 0.45
test_datetime_formatMethod · 0.45
round_tripFunction · 0.45
test_utf16_roundtripMethod · 0.45
test_user_provided_nsMethod · 0.45
test_deepcopyMethod · 0.45
_test_xmlMethod · 0.45
test_add_name_typeMethod · 0.45

Calls 2

to_objMethod · 0.45
exportMethod · 0.45

Tested by 12

test_datetime_formatMethod · 0.36
test_utf16_roundtripMethod · 0.36
test_user_provided_nsMethod · 0.36
test_deepcopyMethod · 0.36
_test_xmlMethod · 0.36
test_add_name_typeMethod · 0.36
_test_xmlMethod · 0.36