Avatar

Please consider registering
guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed Topic RSS sp_TopicIcon
PlainVariable with XmlElement
March 7, 2018
15:36, EET
Avatar
clemens.boehler
Member
Members
Forum Posts: 6
Member Since:
February 2, 2017
sp_UserOfflineSmall Offline

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

March 7, 2018
16:37, EET
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 983
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

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.

March 7, 2018
17:00, EET
Avatar
clemens.boehler
Member
Members
Forum Posts: 6
Member Since:
February 2, 2017
sp_UserOfflineSmall Offline

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

March 7, 2018
17:16, EET
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 983
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

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

March 8, 2018
10:12, EET
Avatar
clemens.boehler
Member
Members
Forum Posts: 6
Member Since:
February 2, 2017
sp_UserOfflineSmall Offline

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

March 9, 2018
8:55, EET
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 983
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

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

Forum Timezone: Europe/Helsinki

Most Users Ever Online: 518

Currently Online:
24 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

hbrackel: 135

pramanj: 86

Francesco Zambon: 81

rocket science: 77

ibrahim: 75

Sabari: 62

kapsl: 57

gjevremovic: 49

Xavier: 43

fred: 41

Member Stats:

Guest Posters: 0

Members: 680

Moderators: 16

Admins: 1

Forum Stats:

Groups: 3

Forums: 15

Topics: 1467

Posts: 6259

Newest Members:

ThomasWilson, elviralangwell4, Donnavek, Eddiefauth, DonaldPooma, fidelduke938316, Jan-Pfizer, DavidROunc, fen.pang@woodside.com, aytule

Moderators: Jouni Aro: 1009, Otso Palonen: 32, Tuomas Hiltunen: 5, Pyry: 1, Petri: 0, Bjarne Boström: 983, Heikki Tahvanainen: 402, Jukka Asikainen: 1, moldzh08: 0, Jimmy Ni: 26, Teppo Uimonen: 21, Markus Johansson: 42, Niklas Nurminen: 0, Matti Siponen: 321, Lusetti: 0, Ari-Pekka Soikkeli: 5

Administrators: admin: 1