UserData List Save as XML

Hi,

I have want to write a list into the userdata of a node. As far as I save this node I got the following exeption:

org.w3c.dom.DOMException: INVALID_CHARACTER_ERR

I put some effort to debug this issue. This issue comes form com.jme3.scene.UserData.
The write methode of this class stores Arrays, Lists and Maps starting with a number:

case TYPE_LIST:
    this.writeList(oc, (List<?>) value, "0");
    break;
case TYPE_MAP:
    Map<?, ?> map = (Map<?, ?>) value;
    this.writeList(oc, map.keySet(), "0");
    this.writeList(oc, map.values(), "1");
    break;
case TYPE_ARRAY:
    this.writeList(oc, Arrays.asList((Object[]) value), "0");
    break;

It seems XML does not allow field names starting with an number.
Can someone confirm this behavoir?

From this page: XML Elements

XML elements must follow these naming rules:

  • Element names are case-sensitive
  • Element names must start with a letter or underscore
  • Element names cannot start with the letters xml (or XML, or Xml, etc)
  • Element names can contain letters, digits, hyphens, underscores, and periods
  • Element names cannot contain spaces
1 Like

Okay. That means the UserData implementation does not follow the xml specification and must be changed.

? UserData isn’t meant to be serialized as XML, why would it have to be changed to accommodate your undertakings?

My suspicion is that he’s trying to use JME’s serialization to XML support… so he’s just wring a Node/S[atial out and this is the error he gets.

Exactly, I am using JME’s serialization for XML and I am geting this error.
I am just save my rootnode. Some of the nodes in my scene graph has Lists in theire userdata. So I am not able to save my game as xml.

As a workaround you could use a Savable object instead of your List/Map.
I guess you need an human readable output for some reason, otherwise i’d tell you to use BinaryExporter instead.

I don’t think the fault lies in UserData, because nowhere is it stated that Savable objects have to follow the XML specification. UserData had no idea that it had to do this.

Actually, XMLExporter is responsible for making sure it generates valid XML given any input. In this case, it can detect that the field name is not valid for XML and “massage” it to fit the specification.