Skip to content

Enclosing XML Tag Name Unavailable as Object Name When Unmarshaled #201

Closed

Description

@MatthewGitter

Hello. When unmarshalling an XML string, eg. var something = unmarshaller.unmarshalString('<thing>Some text.</thing>'); , the overall enclosing XML tag name, ie. <thing>, is not available in the unmarshalled JS object, ie. thing.value.data is unavailable in the JS script. Instead the overall enclosing XML tag name is replaced by the name of the variable coded in the assignment to the return value of the unmarshaller, ie. something , so the value enclosed by the XML is referenced in the JS script by something.value.data . Unless I'm missing something in the unmarshalling process that preserves the outermost enclosing tag name.

var One = {
    name: 'One',
    typeInfos: [{
        type: 'classInfo',
        localName: 'ValueType',
        propertyInfos: [{
            name: 'data',
            type: 'value',
            typeInfo: 'String'
        }]
    }],
    elementInfos: [{
        elementName: 'thing',
        typeInfo: 'One.ValueType'
    }]
};
var context = new Jsonix.Context([One]);
var unmarshaller = context.createUnmarshaller();
var something = unmarshaller.unmarshalString('<thing>Some text.</thing>');
console.log("something:\n");
console.log(something);
console.log("something data value:");
console.log(something.value.data);

Activity

  1. highsource commented on Sep 22, 2018

    @highsource
    Owner

    Have you actually tried looking in something? Because the data you're interested in is there. The qualified name of the element is available as something.name. something.name.localPart gives you thing.

    Here's a Fiddle for this:

    https://jsfiddle.net/lexi/vrkefbd5/

  2. self-assigned this
    on Sep 22, 2018
  3. added this to the 2.4.2 milestone on Sep 22, 2018
  4. MatthewGitter commented on Sep 22, 2018

    @MatthewGitter
    Author

    Yes, I dumped the whole JS object structure, and I saw the enclosing tag name is in the properties tree. I guess that's the only way, since the unmarshaller doesn't have access to the JS script's namespace in which to set the unmarshalled object's name. Though maybe a new method, like unmarshalString(xmlStr, 'something') or unmarshalString(xmlStr, something) might help keep the JS script simpler.

  5. highsource commented on Sep 22, 2018

    @highsource
    Owner

    I'm not quite sure what "unmarshalled object's name" is and what unmarshalString(xmlStr, 'something') should do.

  6. MatthewGitter commented on Sep 23, 2018

    @MatthewGitter
    Author

    In var something = unmarshaller.unmarshalString('<thing>Some text.</thing>'); the unmarshalled object's name is something, in the JS script's namespace. The unmarshaller populates the object the unmarshaller returns, but the unmarshaller doesn't have access to the JS script's namespace, so the unmarshaller can't name the returned object.

    unmarshalString(xmlStr, 'something') can't name the returned object either, I was mistaken to suggest it. But unmarshalString(xmlStr, something) could use an already created something, but that's not better than the existing var something = unmarshaller.unmarshalString('<thing>Some text.</thing>');. Neither suggestion is a good one.

    The only way I can think of to allow the outermost enclosing tag name to be used in the JS script to refer to the unmarshalled object would be for the unmarshaller to return an object that contains the entire unmarshalled property tree as a property. Then the name assigned in the JS script would refer to the object containing the actual unmarshalled object. Eg.

    var something = unmarshaller.unmarshalString('<thing><who><firstName>Some</firstName><lastName>Name</lastName></who><what>Some text.</what></thing>');` 
    var lastName = something.thing.who.lastName;
    
    

    That structure would make walking the property tree reference the outermost container's name in the same way that all the properties in its property tree are referenced, useful in recursion/loops and other logic walking the tree. Other metadata could be stored in the outermost container in a property next to the property tree root, eg. if(something.source === 'string').

  7. highsource commented on Sep 23, 2018

    @highsource
    Owner

    To be honest, I am not quite sure which problem you're trying to solve. Please show a piece of code which does not work now but which you'd like to work.

  8. MatthewGitter commented on Sep 24, 2018

    @MatthewGitter
    Author
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions