15:36, EET
February 2, 2017
Hi everyone,
I’m using the Prosys-OPC-UA-Java-SDK-Client-Server-Binary-2.3.3-783.jar and the Opc.Ua.Stack-1.02.337.18.jar and want to add a PlainVariable with an XmlElement data type to my OPC UA server.
As soon as I want to set the value of this variable I get this exception
ava.lang.RuntimeException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
at org.opcfoundation.ua.builtintypes.XmlElement.getNode(Unknown Source)
at org.opcfoundation.ua.builtintypes.XmlElement.equals(Unknown Source)
at org.opcfoundation.ua.utils.ObjectUtils.objectEquals(Unknown Source)
at com.prosysopc.ua.server.nodes.PlainVariable.setCurrentValue(Unknown Source)
at com.prosysopc.ua.server.nodes.PlainVariable.setCurrentValue(Unknown Source)
at com.prosysopc.ua.server.nodes.PlainVariable.setCurrentValue(Unknown Source)
at at.alphagate.asphere.rt.server.opcua.nodes.OpcUaXmlElementNode.updateNodeValue(OpcUaXmlElementNode.java:107)
at at.alphagate.asphere.rt.server.opcua.nodes.OpcUaBaseDataTypeNode.updateValue(OpcUaBaseDataTypeNode.java:529)
at at.alphagate.ag.AgAbstractItem.fireUpdateValue(AgAbstractItem.java:1288)
at at.alphagate.ag.AgString.setString(AgString.java:434)
at at.alphagate.ag.AgString.setString(AgString.java:393)
at at.alphagate.ag.AgAbstractItem.setString(AgAbstractItem.java:2361)
at at.alphagate.vis.mod.App.startProject(App.java:295)
at at.alphagate.vis.mod.App.bootupApplication(App.java:107)
at at.alphagate.vis.mod.MainFrame$1.run(MainFrame.java:75)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
… 29 more
The XML content that should be written to the variable is stored in a file which is UTF-8 encoded (without BOM). I tried all constructors of the XmlElement (byte[], String and DOM Node) but I always get the same exception.
The file itself is parsed with
try (FileInputStream fileIn = new FileInputStream(new File(“clemens.xml”));
InputStreamReader reader = new InputStreamReader(fileIn, “UTF-8”);)
{
InputSource is = new InputSource(reader);
is.setEncoding(“UTF-8”);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(is);
StringWriter sw = new StringWriter();
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.transform(new DOMSource(doc), new StreamResult(sw));
String val = sw.toString();
…
}
catch (Exception e)
{
e.printStackTrace();
}
Did someone else face these problems (I didn’t find any post here in the forum that sounded similar) or am I missing something?
Thanks in advance for your help and kind regards,
Clemens
16:37, EET
April 3, 2012
Hi,
At least one scenario where this can happen is if the String passed to the XmlElement(String) constructor is empty. In general the input must be a valid XML document, but at the moment the implementation might not throw errors in this case.
Could you please verify that the String you are trying to pass is the file contents and valid XML and not e.g. empty String?
– Bjarne
P.S. unless you specifically need to have the XmlElement type, you could use just a String type instead.
17:00, EET
February 2, 2017
Hi,
I checked if the String is not empty and the XML document is valid so that should not cause the RuntimeException.
I will use a PlainVariable with a String for now but for me that sounds more like a workaround and the XmlElement data type would be more appropriate to represent an XML document…
– Clemens
17:16, EET
April 3, 2012
Could it be possible to paste or send the XML file (or equivalent one that fails) to us?
Is it possible that an earlier value of the node is the one with empty? Because in general the problem is XmlElement.equals which calls XmlElement.getNode, which does give the error (so it could be either of the values). Could you call getCurrentValue() for the node before setting it and checking if that one is empty (calling getNode, it should give the error).
– Bjarne
10:12, EET
February 2, 2017
Hi Bjarne,
that you for your feedback. The initial string value was an empty string. When I do not set the node value in case of empty or null, everything works fine. What would be the appropriate value for the XML element in case of an empty string. Should I simply set the node value to null?
– Clemens
8:55, EET
April 3, 2012
Good question.
To my knowledge, null values should not be used, unless the status is also bad. Also some clients use the existing value as a base for Write, which means on null values they could not determine the type (in general this is the wrong way to do it, the DataType Attribute should be used instead) and therefore could not be used to write values. If you control all clients and servers and they do not fail on null, you can use it.
I cannot find a mention in the spec that would force the value to be well-formed XML. However to my knowledge that is the very definition of XML (i.e. not-well-formed is not XML). Empty string is not-well formed XML as it needs to include one or more element: https://www.w3.org/TR/REC-xml/#sec-well-formed
I recommend having a single selfclosing tag as the “empty” value. If you have a schema common for all XMLs, you should use the top element name. Or if it is nillable, it should be possible to set the xsi:nil attribute of the element to true to indicate a true null value, however I’m not sure all parsers support that.
– Bjarne
Most Users Ever Online: 1919
Currently Online:
17 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Heikki Tahvanainen: 402
hbrackel: 144
rocket science: 88
pramanj: 86
Francesco Zambon: 83
Ibrahim: 78
Sabari: 62
kapsl: 57
gjevremovic: 49
Xavier: 43
Member Stats:
Guest Posters: 0
Members: 729
Moderators: 7
Admins: 1
Forum Stats:
Groups: 3
Forums: 15
Topics: 1529
Posts: 6471
Newest Members:
rondawolinski7, Marypof5711, roycedelargie91, kourtneyquisenbe, ellis87832073466, zkxwilliemae, gabriellabachus, Deakin, KTP25Zof, Wojciech KubalaModerators: Jouni Aro: 1026, Pyry: 1, Petri: 0, Bjarne Boström: 1032, Jimmy Ni: 26, Matti Siponen: 349, Lusetti: 0
Administrators: admin: 1