1

I have found a way to get Google suggestions with XML, Its an address like this:

This is suggestions for the word Cars

My question is how can I get XML to show as a proper HTML ul/li when regeusting with an input field. So typing in input field would get XML list and show it as regular HTML list.

defau1t
  • 10,593
  • 2
  • 35
  • 47
Youss
  • 4,196
  • 12
  • 55
  • 109

2 Answers2

0

You could use XSLT

But you will run into the problem that you cannot just simply import XML to HTML in Internet Explorer.


So, for your purpose just iterate on the relevant Nodes

var xml = new DOMParser( ).parseFromString(/*xml string*/,"text/xml") ;

var l1 = xml.getElementsByTagName("suggestion") ;
var l2 = xml.getElementsByTagName("num_queries") ;

var d = document.createElement("div") ;

    if(l1.length === l2.length) {

         for(var i = 0, u, s, q , e; i < l1.length ; i++) {

             u = document.createElement("ul") ;
             d.appendChild(u) ;

             s = l1[i] ;
             q = l2[i] ;

             e = document.createElement("li") ;
             e.appendChild(  document.createTextNode( s.getAttribute("data") )  ) ;
             u.appendChild(e) ;

             e = document.createElement("li") ;
             e.appendChild(  document.createTextNode( q.getAttribute("int") )  ) ;
             u.appendChild(e) ;

        }

    }

document.getElementsByTagName("body")[0].appendChild(d) ;

  1. Preview on JSBin
FK82
  • 4,907
  • 4
  • 29
  • 42
  • That will only get me the xml for the word 'cars' Im trying to mimic the google suggestions list, so I have to get a different list with every time I press a key in an input field. I already ask this question over here: http://webdeveloper.com/forum/showthread.php?t=248975 But for some reason suddenly it stopt working and I dont know what the problem is..Becouse the xml google is providing is still functional. – Youss Dec 11 '11 at 12:02
  • 1
    @Youss: the output depends on the input XML document. You just need to replace the `xml` variable with the result of any query. The format of the output XML delivered by Google remains the same, hence the code will still work. – FK82 Dec 11 '11 at 12:41
0

If you are trying to include JavaScript into XML here is what you need: https://stackoverflow.com/a/1451526/1065429

If you just want to display text with a bullet point use CSS: display: list-item

Community
  • 1
  • 1
austincheney
  • 1,097
  • 7
  • 8
  • The question, I believe, is how to put XML into HTML not how to put JavaScript code into HTML. – FK82 Dec 11 '11 at 12:44
  • In that case it would have to be XHTML 1.1 and nothing less strict and then since you are already in a natively XML document just add bits from other XML languages using namespaces. – austincheney Dec 11 '11 at 13:57
  • I recommend reading the OP's question again. Hint: he wants HTML output. – FK82 Dec 11 '11 at 14:43