Wikipedia api with xml

mcmanu

Active Member
Licensed User
Longtime User
Hi, i hope somebody can help me a little bit :)

I want to use the Wikipedia api to get the wiki text into my applikation.
For example, i enter into a edittext word "google" and then a label shows the wiki text --> Google Inc. is an American multinational Internet and software corporation specialized in Internet search, cloud computing, and advertising technologies.

I have this xml code -->

B4X:
<SearchSuggestion version="2.0">
<Query xml:space="preserve">india</Query>
−
<Section>
−
<Item>
<Text xml:space="preserve">India</Text>
−
<Description xml:space="preserve">
India (), officially the Republic of India (, '), is a country in South Asia. 
</Description>
<Url xml:space="preserve">http://en.wikipedia.org/wiki/India</Url>
<Image source="http://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Indischer_Maler_des_6._Jahrhunderts_001.jpg/48px-Indischer_Maler_des_6._Jahrhunderts_001.jpg" width="48" height="50"/>
</Item>
−
<Item>
<Text xml:space="preserve">Indiana</Text>
−
<Description xml:space="preserve">
Indiana () is a US state, admitted to the United States as the 19th on December 11, 1816. It is located in the midwestern and Great Lakes regions of North America. 
</Description>
<Url xml:space="preserve">http://en.wikipedia.org/wiki/Indiana</Url>
<Image source="http://upload.wikimedia.org/wikipedia/en/thumb/d/d2/Natives_guiding_french_explorers_through_indiana.jpg/50px-Natives_guiding_french_explorers_through_indiana.jpg" width="50" height="31"/>
</Item>
−
<Item>
<Text xml:space="preserve">Indianapolis</Text>
−
<Description xml:space="preserve">
Indianapolis  (abbreviated Indy ) is the capital of the U.S. 
</Description>
<Url xml:space="preserve">http://en.wikipedia.org/wiki/Indianapolis</Url>
<Image source="http://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/IndianapolisC12.png/34px-IndianapolisC12.png" width="34" height="50"/>
</Item>
−
<Item>
<Text xml:space="preserve">Indian National Congress</Text>
−
<Description xml:space="preserve">
The Indian National Congress () (abbreviated INC, and commonly known as the Congress) is one of the two major political parties in India, the other being the Bharatiya Janata Party (BJP). 
</Description>
−
<Url xml:space="preserve">
http://en.wikipedia.org/wiki/Indian_National_Congress
</Url>
<Image source="http://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Jawaharlal_Nehru.jpg/48px-Jawaharlal_Nehru.jpg" width="48" height="50"/>
</Item>
−
<Item>
<Text xml:space="preserve">Indian Ocean</Text>
−
<Description xml:space="preserve">
The Indian Ocean is the third largest of the world's oceanic divisions, covering approximately 20% of the water on the Earth's surface. 
</Description>
<Url xml:space="preserve">http://en.wikipedia.org/wiki/Indian_Ocean</Url>
<Image source="http://upload.wikimedia.org/wikipedia/commons/thumb/d/d3/Indian_Ocean-CIA_WFB_Map.png/50px-Indian_Ocean-CIA_WFB_Map.png" width="50" height="50"/>
</Item>
−
<Item>
<Text xml:space="preserve">Indianapolis Colts</Text>
−
<Description xml:space="preserve">
The Indianapolis Colts are a professional American football team based in Indianapolis. 
</Description>
<Url xml:space="preserve">http://en.wikipedia.org/wiki/Indianapolis_Colts</Url>
</Item>
−
<Item>
<Text xml:space="preserve">Indiana University</Text>
−
<Description xml:space="preserve">
Indiana University is a multi-campus public university system in the state of Indiana, United States. 
</Description>
<Url xml:space="preserve">http://en.wikipedia.org/wiki/Indiana_University</Url>
<Image source="http://upload.wikimedia.org/wikipedia/en/thumb/5/5d/Indiana_University_seal.svg/50px-Indiana_University_seal.svg.png" width="50" height="50"/>
</Item>
−
<Item>
<Text xml:space="preserve">Indian subcontinent</Text>
−
<Description xml:space="preserve">
The Indian subcontinent, sometimes called the Indo-Pak subcontinent in Pakistan, or simply the Subcontinent, is a region of the Asia situated mostly on the Indian tectonic plate.
</Description>
<Url xml:space="preserve">http://en.wikipedia.org/wiki/Indian_subcontinent</Url>
<Image source="http://upload.wikimedia.org/wikipedia/commons/thumb/0/0e/Indian_subcontinent.JPG/40px-Indian_subcontinent.JPG" width="40" height="50"/>
</Item>
−
<Item>
<Text xml:space="preserve">Indian Rebellion of 1857</Text>
−
<Description xml:space="preserve">
The Indian Rebellion of 1857 began as a mutiny of sepoys of the British East India Company's army on 10 May 1857, in the town of Meerut, and soon escalated into other mutinies and civilian rebellions largely in the upper Gangetic plain and central India, with the major hostilities confined to present-day Uttar Pradesh, Bihar, northern Madhya Pradesh, and the Delhi region. 
</Description>
−
<Url xml:space="preserve">
http://en.wikipedia.org/wiki/Indian_Rebellion_of_1857
</Url>
<Image source="http://upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Indian_Rebellion_of_1857.jpg/50px-Indian_Rebellion_of_1857.jpg" width="50" height="42"/>
</Item>
−
<Item>
<Text xml:space="preserve">India national cricket team</Text>
−
<Description xml:space="preserve">
The Indian cricket team is the national cricket team of India. 
</Description>
−
<Url xml:space="preserve">
http://en.wikipedia.org/wiki/India_national_cricket_team
</Url>
<Image source="http://upload.wikimedia.org/wikipedia/en/thumb/8/8d/Cricket_India_Crest.svg/50px-Cricket_India_Crest.svg.png" width="50" height="50"/>
</Item>
</Section>
</SearchSuggestion>


Or this code i have found in the internet -->

B4X:
http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&titles=United_States_Bill_of_Rights

I know that i can get the wiki text with the xml parse, but how? :-( or do i have to use httputils?

I hope you can help me a little bit, so that i can go further my self
 

mcmanu

Active Member
Licensed User
Longtime User
Hi

Thank you Erel :)
Is there any example where i can get some clues to download a xml file from wiki or other sites? I don´t know how i should start first. I red the example for httputils and xml parse but i know only how to download a picture.
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
The info here will help you, but you need to look for more information at the Wikipedia website as I got the message:


Scripts should use an informative User-Agent string with contact information, or they may be IP-blocked without notice.



When I plugged your URL into the code.

You need to load the HttpUtils and HttpUtilsService module from the attachments of the post above.
 
Upvote 0

mcmanu

Active Member
Licensed User
Longtime User
Hi stevel05

Thank you for the Information, i didn´t know about the user agent
Do you know where i have to put the string in? in the parser or in httputils? and what is the user agent?
I have red the article but ??? xD is it only my app name or is it my email address?
 
Last edited:
Upvote 0

mcmanu

Active Member
Licensed User
Longtime User
Desperate :-(

I dont get it to run
I dont know were the mistake is.

My compiler says:
Compiling code. Error
Error compiling program.
Error description: Unknown member: download
Occurred on line: 35
HttpUtils.Download("Job1", b4a)
Word: download

but why? the sub download exists :BangHead:
 
Upvote 0

mcmanu

Active Member
Licensed User
Longtime User
Thanks

I am such an idiot xD Thank you


Now it works, i get the Wikipedia Text :) Ahhhhh iam so Glad :)


Thank you all for your help ;)
 
Last edited:
Upvote 0

mcmanu

Active Member
Licensed User
Longtime User
Get the result after button click

I now want that i enter for example basic4android into the edittext and than i get the result, i tried to put this -->
HttpUtils.CallbackActivity = "Main" 'Current activity name.
HttpUtils.CallbackJobDoneSub = "JobDone"
HttpUtils.Download("Job1", b4a)

into the button_click event but it doesnt work. is it possible to realize this?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Something like:

B4X:
Dim searchurl as string
searchurl = "http://en.wikipedia.org/w/index.php?title=Special%3ASearch&search="
'OR THIS URL for xml???
searchurl = "http://en.wikipedia.org/w/api.php?action=opensearch&"
Dim searchterm as string
searchterm = edittext1.text 'Or whatever the name of the edittext

Dim format as string
format = "&format=xml"

HttpUtils.Download("Job1", searchurl & searchterm & format)

But I cant help you unless you post some of your project since I do not know how you are getting the xml file.

What is the point in getting the xml rather than the html file? What are you trying to do?
 
Upvote 0

mcmanu

Active Member
Licensed User
Longtime User
Something like:

B4X:
Dim searchurl as string
searchurl = "http://en.wikipedia.org/w/index.php?title=Special%3ASearch&search="
'OR THIS URL for xml???
searchurl = "http://en.wikipedia.org/w/api.php?action=opensearch&"
Dim searchterm as string
searchterm = edittext1.text 'Or whatever the name of the edittext

Dim format as string
format = "&format=xml"

HttpUtils.Download("Job1", searchurl & searchterm & format)

But I cant help you unless you post some of your project since I do not know how you are getting the xml file.

What is the point in getting the xml rather than the html file? What are you trying to do?


Hi, Thank you :)
Iam getting the xml with the httputils, than i parse it that i can get the wikipedia text for example: I put "who is bill gates" into the edittext then after the button click the httputils have to download the xml and then the parser parse the file to get the text --> Bill gates is bla bla bla

here is my project, my problem is that i have to get the results after the button click. but alway when i try to set the httputils.download into the button_click event i get nothing :-(
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Here is a sample that will make it work.
However this is VERY primitive, but it does work.

a. URLEncode: You need a function to encode your query (otherwise spaces and special charachters will escape the url)

b. Add a listview. You are getting multiple search results, you might want to save it into a listview.
 

Attachments

  • w.zip
    8.1 KB · Views: 273
Upvote 0

mcmanu

Active Member
Licensed User
Longtime User
hi

Here is a sample that will make it work.
However this is VERY primitive, but it does work.

a. URLEncode: You need a function to encode your query (otherwise spaces and special charachters will escape the url)

b. Add a listview. You are getting multiple search results, you might want to save it into a listview.

I now get it work :) i used b4a = "http://de.wikipedia.org/w/api.php?action=opensearch&search=" & text2.Text.Replace(" ","&") &"&format=xml"


Last Problem, can i Programm that if i dont get any result that a messagebox is shown with "sorry nothing found"
Can you help me out? i need the funktion when the result was false
or is ist posible to stop parsing with button_click?
 
Last edited:
Upvote 0
Top