0

my xml :

...
<phone type="cell" ext=""></phone>
<phone type="work" ext="">(123) 456 7890</phone>
...

in php; I json_encode($xml) and echo to browser. the browser recieves:

...
"phone": [
    {
        "@attributes": {
            "type": "cell",
            "ext": ""
        }
    }, "(123) 456 7890", {
        "@attributes": {
            "type": "work",
            "ext": ""
        }
    }
]

in javascript

...
var phone_rec = {};
phone_rec.addPhone = function(argument, sender) { 
    function newPhone() {
        this.phone = {};
        this.phone.@attributes = {};
        [email protected] = null;
        [email protected] = null;
    };
    return newPhone();
}...

this falls apart and I am unable to reference this.phone for the number .

2
  • You provided tons of unnecessary details which made answering your question harder. PHP and XML to JSON conversion is absolutely irrelevant, what counts when investigating JavaScript errors is what the browser sees. And quoting the actual JavaScript error that you see is a must. Commented Aug 26, 2011 at 7:56
  • 2
    phone.@attributes is not valid JavaScript. Should be phone['@attributes']. Commented Aug 26, 2011 at 7:56

1 Answer 1

4

That's because you don't call newPhone on your object, by default this will equal to window however. Call it like this instead:

return newPhone.call(this);

See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call for documentation.

In addition, phone.@attributes will produce a syntax error as noted correctly by Felix Kling (identifiers cannot contain @). You should access this property like this:

 this.phone["@attributes"].type = null;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.