Skip to content

Commit 515bc60

Browse files
bitkeeperoroulet
authored andcommitted
[xmlimport] support nillable values
1 parent e184dd3 commit 515bc60

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

opcua/common/xmlparser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def _parse_value(self, val_el, obj):
213213
"""
214214
Parse the node val_el as a constant.
215215
"""
216-
if val_el is not None:
216+
if val_el is not None and val_el.text is not None:
217217
ntag = self._retag.match(val_el.tag).groups()[1]
218218
else:
219219
ntag = "Null"

tests/tests_xml.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,54 @@ def test_xml_custom_uint32(self):
309309
o = self.opc.nodes.objects.add_variable(2, "xmlcustomunit32", 0, varianttype=ua.VariantType.UInt32, datatype=t.nodeid)
310310
self._test_xml_var_type(o, "cuint32")
311311

312+
def test_xml_var_nillable(self):
313+
xml = """
314+
<UANodeSet xmlns="http://opcfoundation.org/UA/2011/03/UANodeSet.xsd" xmlns:uax="http://opcfoundation.org/UA/2008/02/Types.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
315+
<NamespaceUris>
316+
</NamespaceUris>
317+
<Aliases>
318+
<Alias Alias="Boolean">i=1</Alias>
319+
<Alias Alias="String">i=12</Alias>
320+
<Alias Alias="HasTypeDefinition">i=40</Alias>
321+
<Alias Alias="HasComponent">i=47</Alias>
322+
</Aliases>
323+
<UAVariable BrowseName="2:xmlstring" DataType="String" NodeId="ns=2;s=test_xml.string.nillabel" ParentNodeId="i=85">
324+
<DisplayName>xmlstring</DisplayName>
325+
<Description>xmlstring</Description>
326+
<References>
327+
<Reference IsForward="false" ReferenceType="HasComponent">i=85</Reference>
328+
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
329+
</References>
330+
<Value>
331+
<uax:String></uax:String>
332+
</Value>
333+
</UAVariable>
334+
335+
<UAVariable BrowseName="2:xmlbool" DataType="Boolean" NodeId="ns=2;s=test_xml.bool.nillabel" ParentNodeId="i=85">
336+
<DisplayName>xmlbool</DisplayName>
337+
<Description>xmlbool</Description>
338+
<References>
339+
<Reference IsForward="false" ReferenceType="HasComponent">i=85</Reference>
340+
<Reference ReferenceType="HasTypeDefinition">i=63</Reference>
341+
</References>
342+
<Value>
343+
<uax:Boolean></uax:Boolean>
344+
</Value>
345+
</UAVariable>
346+
347+
</UANodeSet>
348+
"""
349+
350+
fp = open('import-nillable.xml', 'w')
351+
fp.write(xml)
352+
fp.close()
353+
# TODO: when the xml parser also support loading from string, remove write to file
354+
_new_nodes = self.opc.import_xml('import-nillable.xml')
355+
var_string = self.opc.get_node(ua.NodeId('test_xml.string.nillabel', 2))
356+
var_bool = self.opc.get_node(ua.NodeId('test_xml.bool.nillabel', 2))
357+
self.assertEqual(var_string.get_value(), None)
358+
self.assertEqual(var_bool.get_value(), None)
359+
312360
def _test_xml_var_type(self, node, typename, test_equality=True):
313361
dtype = node.get_data_type()
314362
dv = node.get_data_value()

0 commit comments

Comments
 (0)