Create empty object from XML schema using JSONIX

Is it somehow possible to create an empty object from XML schema with JSONIX? I usually have problems creating new JS objects that conform to the XML schema. So this would be very helpful. Any example would be much appreciated. I tried to create a new object. NodeType is a complex type name in this case.

{name: {localpart: nodeType}, value:{}};

      

Then I tried to fill in the values ​​(I go through the schema mappings to find out all the possible properties for each type). However, I get, for example, the following error, which doesn't help me much: The [ELEMNAME] element is not known in this context

If this is not possible, how do I create a new object at all that should match the schema?

Thanks for any idea!

EDIT: Ok, here's an example:

"NodeType":{
        "type":"object",
        "title":"NodeType",
        "properties":{
            "id":{
                "title":"id",
                "allOf":[
                    {
                        "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/string"
                    }
                ],
                "propertyType":"attribute",
                "attributeName":{
                    "localPart":"id",
                    "namespaceURI":""
                }
            },
            "x":{
                "title":"x",
                "allOf":[
                    {
                        "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/decimal"
                    }
                ],
                "propertyType":"attribute",
                "attributeName":{
                    "localPart":"x",
                    "namespaceURI":""
                }
            },
            "y":{
                "title":"y",
                "allOf":[
                    {
                        "$ref":"http://www.jsonix.org/jsonschemas/w3c/2001/XMLSchema.jsonschema#/definitions/decimal"
                    }
                ],
                "propertyType":"attribute",
                "attributeName":{
                    "localPart":"y",
                    "namespaceURI":""
                }
            }

      

This is one JSON Schema snippet from my XSD file. I want the object to look like this:

{id:"", x: "", y: ""}

      

The goal is to combine this object into XML.

+3


source to share


1 answer


Disclaimer: I am the author of Jsonix .

If I asked the question correctly, you are asking how you could create a JS object that would then be marshaled with Jsonix.

The trick that most people do is take the "how it should look" XML, reverse it, and log JSON.stringify(myObject, null 2)

something like that. Then you will see how the corresponding JS should look like.

What you can also do is create a JSON schema from your XML schema . This will give you a JSON schema such as this , providing the complete JSON description you need to sort.

I'm also considering implementing something like generating TypeScript classes, but that is future music.

Update



Sorry, I'm still not entirely sure about your question. You've added a JSON schema that defines a complex type NodeType

with strings id

and decimal x

and y

properties.

You are asking what the empty object would be for this. Well, an empty object for your complex type would be simple {}

.
An object with values ​​would be, for example {id:"someId", x: 4, y:2}

.

But in XML, you cannot marshal only a complex type, you marshal an element of a complex type. What is represented as a { name: ..., value: ... }

. So you probably have something like:

{
  "name": {
    "namespaceURI": "uri",
    "localPart": "root"
  },
  "value": {
    "id" : "someId",
    "x" : 4,
    "y" : 2,
    "TYPE_NAME": "NodeType"
  }
}

      

Hope it helps.

+2


source







All Articles