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)
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!
check out the juce Url class it’s got all it needs plus more.
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?
After searching through the forum, would it look something like this?:
void getURL(String iURL)
URL seeqpodResults(T("%s", iURL));
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?).
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
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!
If you want to download a file from the Internet and you know that its an XML file.
Then you can try this.
InputStream* inputStream = url.createInputStream();
I’m using an example URL here that actually points to an XML file.
Then you can:
Also this works too because it is a valid XML element.
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.
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!
Alright, I’ve got everything pretty close to finished. Here’s my code:
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;
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
if(searchResultsXML -> hasAttribute("total"))
// 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">
I Dare You To Move
Learning to Breathe
You Already Take Me There
Learning to Breathe
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!
If you know the number of the XML element you are after, the try using:
Thanks, that’s just what I needed!