How to get HTTP web page data (XML)?


#1

Hey!

For a project I’m trying to complete, I need to get an XML file from a webpage (no parsing, just retrieving the XML from the server over HTTP).

I used to work in wxWidgets, and is has (had, maybe, it’s been a while) an easy way to do this:

void UpdateDlg::WxButton2Click(wxCommandEvent& event)
{
    wxHTTP http;

    http.SetHeader(wxT("Accept"),wxT("/*"));
    http.SetHeader(wxT("User-Agent"),wxT("ADSC"));
    http.SetTimeout(120);

    if(http.Connect("ADServerChecker.googlepages.com",80));
    {
        if(wxInputStream *stream=http.GetInputStream(wxT("/version.xml")))
        {
            wxString data;
            wxStringOutputStream out_stream(&data);
            stream->Read(out_stream);
            delete stream;
        }
    }
}

How would I achieve a similar connection through Juce? I looked at InterprocessConnection, but (and this might be because I haven’t done much coding lately, I’m somewhat rusty) it just doesn’t seem clear how I should open a connection and receive data.

Since I would love to sort of figure it out on my own (instead of being spoon-fed code). Are there any similar examples of others that I could use to look at?

Thanks for your abounding help!
FlyingIsFun1217


#2

check out the juce Url class it’s got all it needs plus more.


#3

Alright…

Wow, looks like there’s even a function to download an xml file (readEntireXmlStream()).

Thing is, I’m still not sure as to how to use it. The documentation only shows one function argument, whether or not a url should be downloaded using a POST command.

Are there any examples of it’s usage?

FlyingIsFun1217


#4

After searching through the forum, would it look something like this?:

#include "juce.h"

void getURL(String iURL)
{
	URL seeqpodResults(T("%s", iURL));
	seeqpodResults.readEntireXmlStream(true);
}

Even if that’s somewhat close, I’m still not sure how I should go about saving the xml data to a variable (from which I would parse, or… is there an xml parser built-in?).

Thanks again!
FlyingIsFun1217


#5

the readEntireXmlStream(true); will return an XmlElement object witch you can use as described in the XmlElement manual:

if (myElement->hasTagName ("ANIMALS"))
    {
        // now we'll iterate its sub-elements looking for 'giraffe' elements..
        forEachXmlChildElement (*myElement, e)
        {
            if (e->hasTagName ("GIRAFFE"))
            {
                // found a giraffe, so use some of its attributes..

                String giraffeName  = e->getStringAttribute ("name");
                int giraffeAge      = e->getIntAttribute ("age");
                bool isFriendly     = e->getBoolAttribute ("friendly");
            }
        }
    }

where myElement will be

XmlElement *myElemet = seeqpodResults.readEntireXmlStream(true); 

and that’s it


#6

Alright, thanks for the examples.

I’ll start working on that later, right now I’m trying to learn this whole Components that inherit Components that inherit Components…

Thanks for your help!
FlyingIsFun1217


#7

If you want to download a file from the Internet and you know that its an XML file.

Then you can try this.

[code]URL url(T(“http://xml.ascc.net/test/wf/utf-8/text_xml/zh-utf8-0.xml”));

InputStream* inputStream = url.createInputStream();

[/code]

I’m using an example URL here that actually points to an XML file.
http://xml.ascc.net/test/wf/utf-8/text_xml/zh-utf8-0.xml

Then you can:

Also this works too because it is a valid XML element.

[code]URL url(T(“http://xml.ascc.net/test/wf/utf-8/text_xml/zh-utf8-0.xml”));

XmlElement* xmlElement = url.readEntireXmlStream();[/code]

This is probably better if you want to do anything with the data provided by the XML file.

So the above is for a simple xml file download or any file for that matter, xml, jpg, gifs, etc.

If your calling a web service then use:

Obviously you have to know the web service function name parameter names and values.

And I think the URL class is also valid if your web service has multiple parameters, because if im not mistaken the URL::withParameter function adds you parameter name and values to a StringPairArray.


#8

Well, I might end up using the download method later on to actually… download and save a stream to a file, when I need to download files described in the XML file.

Thanks for the heads up!
FlyingIsFun1217


#9

Alright, I’ve got everything pretty close to finished. Here’s my code:

class slResults
{
	private:
		
		URL seeqpodResults;
		
		XmlElement *searchResultsXML;
		
		int numOfResults;
		
		String createdURL;
		
	public:
	
		void createURL(String iText)
		{
			// Still need to parse anything with spaces to replace spaces with '%20'
			createdURL = "http://www.seeqpod.com/api/music/anonSearchPaginationFlash?s=0&n=50&q=" + iText;
		}
	
		slResults(String iURL)
		{
			seeqpodResults = new URL(T("%s", iURL));
			searchResultsXML = seeqpodResults.readEntireXmlStream(true);
		}
		
		String getElement(int resultNum, String elementDescription)
		{
			if(searchResultsXML -> hasTagName("playlist"))
			{
				if(searchResultsXML -> hasTagName("trackList"))
				{
					// Go to 'elementDescription' number 'resultNum', and get result, pass back as String-type
				}
			}
		}
		
		int getNumTrackResults()
		{
			if(searchResultsXML -> hasAttribute("total"))
			{
				return searchResultsXML.getIntAttribute("total");
			}
			
			else
			{
				// Print error that there was no 'total' attribute found in document
			}
		}
};

This is to read a file pretty much similar to this:

<playlist version="1" start="0" end="49" total="169" prev="None" next="50">
	<trackList>
		<track>
			<location>
				http://www.thisurl.com/DYTM.mp3
			</location>
			<title>
				I Dare You To Move
			</title>
			<creator>
				Switchfoot
			</creator>
			<album>
				Learning to Breathe
			</album>
		</track>
		<track>
			<location>
				http://www.thisurl.com/YATMT.mp3
			</location>
			<title>
				You Already Take Me There
			</title>
			<creator>
				Switchfoot
			</creator>
			<album>
				Learning to Breathe
			</album>
		</track>
	</trackList>
</playlist>

Now, as you can see… my function ‘getElement’ is not quite complete. Basically, what I’d like to do is let the user say, "Hey, I’d like to get the result for element ‘album’ (or ‘creator’, whatever) at it’s Xth occurance.

Is there a way to get the Xth XML element in a document with more than one? Would it be just a loop through getting the ‘next’ one each time until I would reach the Xth?

Thanks again for your awesome help!
FlyingIsFun1217


#10

If you know the number of the XML element you are after, the try using:


#11

Thanks, that’s just what I needed!

FlyingIsFun1217 :slight_smile: