Using JQuery AJAX, I send HTTP request to PHP back-end and tried to retrieve XML String parsed into JSON using json_encode()
function back to the front-end. Some of the XML contains attributes, which show @attributes
in the output. Somehow, I tried to console.log()
the @attributes
but failed. Here is the detail:
In front-end, using JQuery AJAX, I send HTTP request to one of my PHP file.
In back-end PHP, I retrieve data from DB where one of the column contains String of XML.
Then, after retrieving, I used
simplexml_load_string()
function to convert the String of XML into true XML.Here is the example of String of XML:
<dsconnector>
<statement>
SELECT SUM(a.doc_amt) FROM gl_temp_trx_dt as a INNER JOIN gl_temp_trx_hd as b ON a.gl_hd_unique_id = b.unique_id
</statement>
<filter id="company123" range="false">
<label>Company</label>
<table>company</table>
<field>company_code</field>
<alias>a</alias>
<desc>company_name</desc>
<dependee>glacc, glyear, glperiod</dependee>
</filter>
</dsconnector>
Notice that <filter>
has two attributes
After that, I used
json_encode()
function to output the converted XMLIn front-end, I used
console.log()
function to see what the output looks like, and here is part of the result:
0: {…}
"@attributes": {…}
id: "companyFilterId"
range: "false"
<prototype>: Object { … }
alias: "ar"
dependee: "projectFilterId_0123, phaseFilterId_ABCD, debtorAccFilterId"
desc: "company_name"
field: "company_code"
label: "Company"
table: "company"
<prototype>: Object { … }
...
- As you can see, the attributes part contains "@". And by using this code to print the result:
...[other AJAX part]...
success: function(data, textStatus, XMLHttpRequest)
{
if (data.status==1)
{
console.log(data.result[0].attributes);
}
else if(data.status==0)
{
}
},
...[other AJAX part]...
It will say that that the attributes
is undefined.
My question is, how to display the JSON Array that the object named contains "@". Like in this case, it show "@attributes". Thanks in advance.