Help with XML+HTML script

Discussion in 'HTML & Website Design' started by techieanish, Oct 30, 2011.

  1. #1
    I am trying to create an HTML page with will tell me an answer to the question using the trueknowledge API.
    Everytime, I ask a question, the URL goes like this:
    https://api.trueknowledge.com/direct_answer?object_metadata=image128,wikipedia,official &question=What Time is it in chicago?&api_account_id=api_testingwork&api_password=testingwork
    The service answers my question in an XML format like this:
    [FONT=arial]<[/FONT][FONT=arial]tk:response[/FONT][FONT=arial]answered[/FONT][FONT=arial]=[/FONT][FONT=arial]"[/FONT][FONT=arial]true[/FONT][FONT=arial]"[/FONT][FONT=arial]type[/FONT][FONT=arial]=[/FONT][FONT=arial]"[/FONT][FONT=arial]direct_answer[/FONT][FONT=arial]"[/FONT][FONT=arial]understood[/FONT][FONT=arial]=[/FONT][FONT=arial]"[/FONT][FONT=arial]true[/FONT][FONT=arial]"[/FONT][FONT=arial]xmlns[/FONT][FONT=arial]=[/FONT][FONT=arial]"[/FONT][FONT=arial]http://www.w3.org/1999/xhtml[/FONT][FONT=arial]"[/FONT][FONT=arial]xmlns:tk[/FONT][FONT=arial]=[/FONT][FONT=arial]"[/FONT][FONT=arial]http://www.trueknowledge.com/ns/kengine[/FONT][FONT=arial]"[/FONT][FONT=arial]>[/FONT][FONT=arial]
        [/FONT][FONT=arial]<[/FONT][FONT=arial]tk:status[/FONT][FONT=arial]>[/FONT][FONT=arial]completeness unknown[/FONT][FONT=arial]</[/FONT][FONT=arial]tk:status[/FONT][FONT=arial]>[/FONT][FONT=arial]
        [/FONT][B][FONT=arial]<[/FONT][FONT=arial]tk:text_result[/FONT][FONT=arial]>[/FONT][FONT=arial]March 20th 2009, 08:09:34 CDT[/FONT][FONT=arial]</[/FONT][FONT=arial]tk:text_result[/FONT][FONT=arial]>[/FONT][/B][FONT=arial]
        [/FONT][FONT=arial]<[/FONT][FONT=arial]tk:structured_result[/FONT][FONT=arial]>[/FONT][FONT=arial]
            [/FONT][FONT=arial]<[/FONT][FONT=arial]tk:result[/FONT][FONT=arial]>[/FONT][FONT=arial]
                [/FONT][FONT=arial]<[/FONT][FONT=arial]tk:object[/FONT][FONT=arial]>[/FONT][FONT=arial]
                    [/FONT][FONT=arial]<[/FONT][FONT=arial]tk:id[/FONT][FONT=arial]>[/FONT][FONT=arial][local timepoint: [timepoint: [[/FONT][FONT=arial]&quot;[/FONT][FONT=arial]2009/3/20/8/9/34[/FONT][FONT=arial]&quot;[/FONT][FONT=arial]}; [central daylight time}[/FONT][FONT=arial]</[/FONT][FONT=arial]tk:id[/FONT][FONT=arial]>[/FONT][FONT=arial]
                [/FONT][FONT=arial]</[/FONT][FONT=arial]tk:object[/FONT][FONT=arial]>[/FONT][FONT=arial]
            [/FONT][FONT=arial]</[/FONT][FONT=arial]tk:result[/FONT][FONT=arial]>[/FONT][FONT=arial]
            [/FONT][FONT=arial]<[/FONT][FONT=arial]tk:question_entities[/FONT][FONT=arial]>[/FONT][FONT=arial]
                [/FONT][FONT=arial]<[/FONT][FONT=arial]tk:object[/FONT][FONT=arial]>[/FONT][FONT=arial]
                    [/FONT][FONT=arial]<[/FONT][FONT=arial]tk:id[/FONT][FONT=arial]>[/FONT][FONT=arial][chicago][/FONT][FONT=arial]</[/FONT][FONT=arial]tk:id[/FONT][FONT=arial]>[/FONT][FONT=arial]
                    [/FONT][FONT=arial]<[/FONT][FONT=arial]tk:metadata[/FONT][FONT=arial]parameter[/FONT][FONT=arial]=[/FONT][FONT=arial]"[/FONT][FONT=arial]image128[/FONT][FONT=arial]"[/FONT][FONT=arial]>[/FONT][FONT=arial]http://www.trueknowledge.com/images/thumbs/128/128/2004-07-14_2600x1500_chicago_lake_skyline.jpg[/FONT][FONT=arial]</[/FONT][FONT=arial]tk:metadata[/FONT][FONT=arial]>[/FONT][FONT=arial]
                    [/FONT][FONT=arial]<[/FONT][FONT=arial]tk:metadata[/FONT][FONT=arial]parameter[/FONT][FONT=arial]=[/FONT][FONT=arial]"[/FONT][FONT=arial]wikipedia[/FONT][FONT=arial]"[/FONT][FONT=arial]>[/FONT][FONT=arial]http://en.wikipedia.org/wiki/Chicago[/FONT][FONT=arial]</[/FONT][FONT=arial]tk:metadata[/FONT][FONT=arial]>[/FONT][FONT=arial]
                [/FONT][FONT=arial]</[/FONT][FONT=arial]tk:object[/FONT][FONT=arial]>[/FONT][FONT=arial]
            [/FONT][FONT=arial]</[/FONT][FONT=arial]tk:question_entities[/FONT][FONT=arial]>[/FONT][FONT=arial]
        [/FONT][FONT=arial]</[/FONT][FONT=arial]tk:structured_result[/FONT][FONT=arial]>[/FONT][FONT=arial]
        [/FONT][FONT=arial]<[/FONT][FONT=arial]tk:retranslation[/FONT][FONT=arial]>[/FONT][FONT=arial]Which local timepoints are the current local time in the US city of Chicago, Illinois at the current time?[/FONT][FONT=arial]</[/FONT][FONT=arial]tk:retranslation[/FONT][FONT=arial]>[/FONT][FONT=arial]
    [/FONT][FONT=arial]</[/FONT][FONT=arial]tk:response[/FONT][FONT=arial]>[/FONT]
    Code (markup):
    the text between the tk:text_result tag is what I want to be printed on my html page. And also, this should be applicable for any questions asked. I got how to process the question, but printing just that answer is troubling me.
    So, is there a way to extract that text from there and print it on the same html page? Also I want to hide the XML from the page and print only the answer. Also, try to make your text noob-safe, because I am not actually a good programmer :D.
     
    techieanish, Oct 30, 2011 IP
  2. oortcloud

    oortcloud Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You'll have to use a programming language capable of processing the XML. Depending on your needs you could do this on the server before the page is sent (better for SEO) with something like PHP or do it in the user browser while the page is loading with something like javascript.

    My recommendation would be to search 'php SimpleXML' or 'jquery parseXML' and you will find the respective documentation that should get you started with one or the other. In the former case your server will need to be running Linux with PHP and in the latter case, you will need to include the jQuery javascript library script in the <head> of your page (instructions on how can be found by searching)
     
    oortcloud, Oct 31, 2011 IP
  3. hdewantara

    hdewantara Well-Known Member

    Messages:
    540
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #3
    Hi Techieanish.

    Sadly I'm not familiar with the API from Trueknowledge.com, but I think there should be a way around. Say that XML response goes to a specific HTML element of your page, then could you hide this element using CSS? Then, you might use javascript to filter the content of this element, and display it to other visible element.

    Well... if you could show me either a link/ URL to your page or the script here, I might be more helpful.

    Hendra
     
    hdewantara, Oct 31, 2011 IP