简体   繁体   中英

XML2JSON and retrieving the data

Trying to make work this code.

Using the function to convert XML 2 JSON

Sample Here:

<?php
class XmlToJson {
    public static function Parse($url) {
        $fileContents= file_get_contents($url);
        $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
        $fileContents = trim(str_replace('"', "'", $fileContents));
        $simpleXml = simplexml_load_string($fileContents);
        $json = json_encode($simpleXml);
        return $json;
    }

}

$getXML = "http://api.devstate.de/json/divisas.xml";

    print XmlToJson::Parse($getXML);

?>

(By the way , the original code have some issues of malformed JSON so we add strict to the function)

Seems that everything works correctly until this step.

We get a valid JSON, but when we tried to call it via jQuery or Angular we always get a UNDEFINED Data .

If I use:

$(document).ready(function() {
    $.getJSON('convertedXML.php', function(json) {
        console.log(json.row);

    });
});

I get:

https://www.dropbox.com/s/oq5a8av2etcwvo3/Screenshot%202015-04-08%2020.02.56.png

But what about json.row.$WHATTHEHELL.Divisa

Nothing happen , nothing is given back just UNDEFINED

What do you think? , what are we doing wrong.

thanks

Inside the $.getJSON block, if you're getting the correct response, then just use $.each to iterate inside it:

$.each(json.row, function(i, e){
    console.log(e['@attributes'].Divisa); // and other properties
});

Sample Demo

@Ghost the solution was fantastic! thanks.

Just , any suggestion for a simplification here:

<div>
    <ul id="currency-USD">
        <li class="currency-USD--name"></li>
        <li class="currency-USD--buy"></li>
        <li class="currency-USD--sell"></li>
    </ul>

    <ul id="currency-UE">
        <li class="currency-UE--name"></li>
        <li class="currency-UE--buy"></li>
        <li class="currency-UE--sell"></li>
    </ul>

    <ul id="currency-CAD">
        <li class="currency-CAD--name"></li>
        <li class="currency-CAD--buy"></li>
        <li class="currency-CAD--sell"></li>
    </ul>
</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script>
    $(document).ready(function(){

        $.ajax({
            url: document.URL,
            type: 'POST',
            dataType: 'JSON',
            success: function(json) {
                var group = json.row;

                // alert('check the browser console');
                $('.currency-USD--name').html(group[0]['@attributes'].Divisa);
                $('.currency-USD--buy').html(group[0]['@attributes'].Compra);
                $('.currency-USD--sell').html(group[0]['@attributes'].Venta);

                $('.currency-UE--name').html(group[1]['@attributes'].Divisa);
                $('.currency-UE--buy').html(group[1]['@attributes'].Compra);
                $('.currency-UE--sell').html(group[1]['@attributes'].Venta);

                $('.currency-CAD--name').html(group[2]['@attributes'].Divisa);
                $('.currency-CAD--buy').html(group[2]['@attributes'].Compra);
                $('.currency-CAD--sell').html(group[2]['@attributes'].Venta);

                    // console.log(group[0]['@attributes'].Divisa);
                }
            });
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM