Get the data from an XML file

colinseu's Avatar

colinseu

30 Apr, 2010 11:51 PM

Hi, first thanks for writing this slick plugin, i'm trying to fit it into my projet...

I would like to know if there were a simple way to fecth the data from an XML file.

So idealy, i'm trying to pull this data :

var data = {items: [
{value: "21", name: "Mick Jagger"},
{value: "43", name: "Johnny Storm"},
{value: "46", name: "Richard Hatch"},
]};

... from this file :

<?xml version="1.0" encoding="UTF-8"?>
<markers>
    <marker value="21" name="Mick Jagger" />
    <marker value="43" name="Johnny Storm" />
    <marker value="46" name="Richard Hatch" />
</markers>

This is what i get so far, but i'm stuck there...

$(document).ready(function() {

    $(function(){

        $.ajax({
            type: "GET",
            url: "markers.xml",
            dataType: "xml",
            success: parseXml
        });

        function parseXml(xml)
        {

            // where parsing should go

            var data = $(xml).find("marker").each(function(){ });



            $("input[type=text]").autoSuggest(data.items, {

                selectedItemProp: "name",
                searchObjProps: "name",
                asHtmlID: "nombres",
                selectionLimit: 1,
                selectionAdded: function(){ showStreet(); },
                selectionRemoved: function(elem){ elem.fadeTo("fast", 0, function(){ elem.remove(); }); cleanMap();}

            }
        );
    }
});
});

Thanks for the help...

Colin.

  1. 1 Posted by Michael Lykke on 19 Sep, 2010 04:56 PM

    Michael Lykke's Avatar

    I'm trying to do the same (sort of), except I'm parsing the xml with php and I'm also having some trouble getting it all to work. Did you ever get any further with this? What were your problems?

    My problem is that no matter what I type in the input box, I get "No results"... My php script returns this when searching for 'jer':

    [{"value":162,"name":"Råbjerg Kirke"},{"value":677,"name":"Katrinebjergskolen"},{"value":706,"name":"Lisbjerg Kirke"},{"value":1033,"name":"Skydebjerg Kirke"},{"value":1263,"name":"Aftenstjernen"},{"value":1295,"name":"Esbjerg Aktivitetshuset"},{"value":1736,"name":"Solbjerg Kirke"},{"value":1914,"name":"Baunebjergskolen"},{"value":1967,"name":"Fjerritslev Bibliotek"},{"value":2042,"name":"Kajerødskolen"}]

    Anyone see any problem with this json_encoded output?

    Best regards, Michael

  2. 2 Posted by Michael Lykke on 19 Sep, 2010 05:09 PM

    Michael Lykke's Avatar

    By the way, I'm using this code to generate the json encoded output:

    function searchArray($sword,$frombeginning=false,$find=10)
    {

    $results = array();
    $matches = 0;
    
    if($frombeginning)
    {
        $pattern = "/^" . $sword . "/i";
    }
    else
    {
        $pattern = "/" . $sword . "/i";
    }
    
    foreach($_SESSION["venues"] as $key=>$value)
    {
        if(preg_match($pattern,$value))
        {
            $results['venue']['value'] = $key;
            $results['venue']['name'] = $value;
            $data[] = $results['venue'];
            $matches++;
    
            if($matches == $find) break;
        }
    }
    
    return $data;

    }

    if(!empty($_SESSION["venues"]))
    {

    $finds = searchArray($_GET["q"]);
    
    echo json_encode($finds);

    }

    I'm thinking that maybe the problem is that the output is wrapped in a function...?

  3. 3 Posted by Maarten K on 05 Nov, 2010 02:25 PM

    Maarten K's Avatar

    Hi

    I'm totally new to JS but I have this working. :-)

    My xml looks like:

    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <string>frequent</string> <string>daadwerkelijk</string> <string>fout</string> </ArrayOfString>

    And the script i've made is:

    $(document).ready(function() {

        $(function(){
            if (window.XMLHttpRequest)
            {
                xmlhttp=new XMLHttpRequest();
            }
            else
            {
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
    
            xmlhttp.open("GET","xml/index.xml",false);
            xmlhttp.send();
            xmlDoc=xmlhttp.responseXML; 
    
            var data = {items:[]};
            for (var i = 0; i < xmlDoc.getElementsByTagName("string").length; i++) 
            {  
                data.items.push({ value: xmlDoc.getElementsByTagName("string")[i].childNodes[0].nodeValue, name: xmlDoc.getElementsByTagName("string")[i].childNodes[0].nodeValue });
            }
    
            $("input[type=text]").autoSuggest(data.items, {selectedItemProp: "name", searchObjProps: "name"});
    
    });

    });

    I use this in an asp.NET application and works great.

    Hope it helps you.

    Best regards,
    Maarten

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac