View Full Version : Getting specific information from XML
nicoh
10-23-2008, 05:41 PM
Hi, I have many XML files that have the next format:
<songs>
<song path="LINK.MP3" bild="img/s.jpg" artist="NAME OF THE ARTIST" title="SONG TITLE"/>
</songs>
The problem is that I don't know how to "extratc" that information to use it into my project.
I need to get them all (but the most important is the "song path".
PD: This will be easier is the structure of the XML was something like:
<song>
<path>LINK.MP3</path>
<artist>ARTIST</artist>
<title>TITLE</title>
</song>
Bye and thanks
jassing
10-23-2008, 06:21 PM
I didn't try anything; but I don't think the built in XML object will hanlde self-ending objects nicely
<start something=something/>
are the xml files "individual" (ie one xml file has one path?)
Dermot
10-23-2008, 06:28 PM
You will need to use the GetAttribute action. Look at the help topic for XML. GetAttribute in the help file for an example.
So in your case, to get the title of the second song you would use.
title = XML.GetAttribute("songs/song:2", "title");
nicoh
10-23-2008, 08:12 PM
Well Dermot, thank you.
Im gonna try with the GetAtribute action :)
Thanks again !
nicoh
10-23-2008, 08:51 PM
Hi, Im here again.
Im doing some test before start building the app.
Ive created a new project and then Ive added an Input, a button and a label.
In the input I will put the URL of the XML file.
The button will collect that URL (onClick) and will put one atribute of that XML on the label.
When I execute the app, the label gets white (without content).
Ive used this code for the onClick event on the button:
urlXML = Input.GetText("Input1"); // gets the URL of the XML
XML.Load("urlXML"); // loads the XML file
getURL = XML.GetAttribute("songs/song:2", "path"); // get the atribute
Label.SetText("Label1", getURL); // Writes the info on the label
There ir something wrong or Ive missed something?
Bye and thanks
Dermot
10-23-2008, 10:54 PM
Are you trying to open an xml file located on the web? If so then that's your problem. You will need to download the file and save to your local drive, then load it.
nicoh
10-23-2008, 11:19 PM
Oh, its okay.
Whats the better way to do that?
Will be great, if is possible to download it to a temporary folder and delete it when u close the app or when Windows is turned off.
jassing
10-23-2008, 11:39 PM
Oh, its okay.
Whats the better way to do that?
Will be great, if is possible to download it to a temporary folder and delete it when u close the app or when Windows is turned off.
Have a look at HTTP.Download(), from there you can download a file to the _TempFolder
so something like
HTTP.Download( cXMLURL, _TempFolder.."\\Temp.xml");
from there you can use the XML object & then use File.Delete() to remove the xml temp file...
nicoh
10-24-2008, 04:16 AM
Hey jassing, thanks for helping me.
With the tip that you gave me, Ive done this:
obtURL = Input.GetText("urlInput"); // gets the xml url from an input
HTTP.Download("obtURL", _TempFolder.."\\mp3.xml"); // downloads it to the hd
obtMP3 = XML.Load(_TempFolder.."\\mp3.xml"); // loads it
urlMP3= XML.GetAttribute(_TempFolder.."\\mp3.xml", "songs/song:2", "path"); // gets the attribute
Label.SetText("Label1", urlMP3); // writes the attrib. in a label object.
I still having the same problem (nothing is shown in the label).
I think that Im making a mistake when Im obtaining / loading the URL:
obtMP3 = XML.Load(_TempFolder.."\\mp3.xml");
urlMP3= XML.GetAttribute(_TempFolder.."\\mp3.xml", "songs/song:2", "path");
Is this right?
Thanks again
Dermot
10-24-2008, 09:21 AM
I guess you didn't look at my previous example or the help file.
urlMP3= XML.GetAttribute("songs/song:2", "path");
nicoh
10-24-2008, 10:18 AM
I guess you didn't look at my previous example or the help file.
urlMP3= XML.GetAttribute("songs/song:2", "path");
Of course I did, but Ive got the same error so Ive tried loading the action directly from the AMS Wizard.
Dermot
10-24-2008, 11:32 AM
You have this.
urlMP3= XML.GetAttribute(_TempFolder.."\\mp3.xml", "songs/song:2", "path");
When you should have this.
urlMP3= XML.GetAttribute("songs/song:2", "path");
nicoh
10-24-2008, 02:35 PM
You have this.
urlMP3= XML.GetAttribute(_TempFolder.."\\mp3.xml", "songs/song:2", "path");
When you should have this.
urlMP3= XML.GetAttribute("songs/song:2", "path");
Yes, Ive tried with both and I still having the same error :S
Centauri Soldier
10-24-2008, 03:08 PM
Often, errors in XML parsing occur because of a syntax error in the XML file itself.
For instance, if you are missing a close tag, none of the XML call functions will work properly. Also, if you don't have quotes around an attribute, you will get errors.
My recommendation is to make an XML file with as little information as possible in order to test your program.
for example:
<songs>
<song title="My Song" extension=".mp3">
</song>
</songs>
Start with something simple like that, and errors will be easier to find.
I have a program that generates thousands of lines of code for one XML file (galaxy generator, including start systems and planets) and then can read lines back when listing statistics. I had errors like yours when I started and used the above-mentioned method to find them. Start simple, then build you process.
Keep Dermot's quote in mind when building any complex system.
jassing
10-24-2008, 04:41 PM
What is the error your are getting?
Do you have control over the xml format?
does one xml file have more than one mp3 information?
nicoh
10-24-2008, 06:05 PM
Hi again.
Im using an XML file from an external website (is a music streaming website) and Im making an app to download the MP3 files that are hosted on it (lets suppose that the MP3 haven't got author rights to avoid problems).
The URLs of the MP3 are on the XML files.
EXAMPLE: http://www.goear.com/files/xmlfiles/0/secm03f60d4.xml
PD: I saw the error Centauri Soldier, the tag is something like <song attributes /> and it must to be <song attributes></song>, there is any way to work with it, anyway? because I have not control over the files and I can't change them.
jassing: Im not getting an explicit error. Im just not getting the result in the label (of course the label is just to test if the information is taken fine or not).
Well, thanks again (and again)
thisisauniqueusername
10-24-2008, 07:00 PM
2 problems with your code:
urlXML = Input.GetText("Input1");
XML.Load("urlXML"); drop the quotes - the quotes make it a string not a variable containing the value
getURL = XML.GetAttribute("songs/song:2", "path"); --drop the :2 - It's only valid when you have more than one Element with the same name
Label.SetText("Label1", getURL);
---
Corrected looks like:
obtURL = Input.GetText("urlInput");
HTTP.Download(obtURL, _TempFolder.."\\mp3.xml");
obtMP3 = XML.Load(_TempFolder.."\\mp3.xml");
urlMP3= XML.GetAttribute("songs/song", "path");
Label.SetText("Label1", urlMP3);
Tested with:
http://www.goear.com/files/xmlfiles/0/secm03f60d4.xml
nicoh
10-24-2008, 07:05 PM
2 problems with your code:
urlXML = Input.GetText("Input1");
XML.Load("urlXML"); drop the quotes - the quotes make it a string not a variable containing the value
getURL = XML.GetAttribute("songs/song:2", "path"); --drop the :2 - It's only valid when you have more than one Element with the same name
Label.SetText("Label1", getURL);
---
Corrected looks like:
obtURL = Input.GetText("urlInput");
HTTP.Download(obtURL, _TempFolder.."\\mp3.xml");
obtMP3 = XML.Load(_TempFolder.."\\mp3.xml");
urlMP3= XML.GetAttribute("songs/song", "path");
Label.SetText("Label1", urlMP3);
Tested with:
http://www.goear.com/files/xmlfiles/0/secm03f60d4.xml
Thanks :) It works fine now.
Im gonna make the test of downloading the mp3 and some other things to make the app functional.
nicoh
10-24-2008, 08:36 PM
Hi again. Im just finishing my app and I have a last (I hope) question.
I have an input where the user inserts a text (an URL in fact).
But, I need to get a "part" of that URL.
Lets suppose that the user inserts "http://mysite.com/mifile.php?=812345".
I need to get two numbers from there. First of all the "812345", and from that number I need to get the firs number (in this case the "8").
Have u got an idea?
nicoh
10-25-2008, 12:34 AM
Well, after searching Ive founded a solution to my problem.
obtURL = Input.GetText("urlInput"); -- gets the url
stringLength = String.Length(obtURL); -- gets the quantity of chars of the string (url)
patternPos = String.Find(obtURL, "=", 1, false); -- search for a pattern (a place where "cut" the string
codMP3 = String.Right(obtURL, stringLength - patternPos); -- creates a new string with the characters at the right of the pattern
catMP3 = String.Left(codMP3, 1); -- gets the new string and makes a new one with the first char of the old string
Well, I have my new app running, I will finish with the details and Ill give you news soon.
THANKS FOR ALL, YOU WERE VERY VERY HELPFUL!
longedge
10-25-2008, 03:01 AM
Post deleted.
S0mbre
12-26-2008, 02:24 AM
Hi all!
This tiny project shows how you can use XML files to read / write label properties and object scripts.
Enjoy!
7482
GoOgLe
01-14-2009, 01:37 AM
Hi all!
This tiny project shows how you can use XML files to read / write label properties and object scripts.
Enjoy!
7482
hi to all how can i make it show random links from xml file ???
S0mbre
01-19-2009, 12:46 AM
Here you go!
7574
GoOgLe
01-19-2009, 01:58 AM
thanks alot S0mbre :yes
S0mbre
01-19-2009, 11:10 PM
There's one more, added ability to set properties according to the random link.
7577
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.