# Loading external xml

**URL:** https://forum.kirupa.com/t/loading-external-xml/261894
**Category:** flash
**Created:** [June 1, 2008, 12:28pm UTC](https://forum.kirupa.com/t/loading-external-xml/261894 "2008-06-01T12:28:52Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![luka\_luka](https://avatars.discourse-cdn.com/v4/letter/l/71c47a/32.png) [@luka\_luka](https://forum.kirupa.com/u/luka_luka)
#### Post date: [June 1, 2008, 12:28pm UTC](https://forum.kirupa.com/t/loading-external-xml/261894/1 "2008-06-01T12:28:52Z")

</div>

I read this tutorial, and it’s beautiful:

[http://www.kirupa.com/developer/flashcs3/using\_xml\_as3\_pg1.htm](http://www.kirupa.com/developer/flashcs3/using_xml_as3_pg1.htm)

It has everything I need but does anybody know how to add images to this? Ability of having little image, next to text. Like a book cover, in this particulary example.  
How to alter both for xml and as code. Would xml be something like this:

\<Books\>  
\<Book ISBN=“xxxx”\>  
\<title\> Title of book 1\</title\>  
\<author\> Author 1\</author\>

```
     &lt; image &gt;
     &lt;title&gt;Title1&lt; /title &gt;
     &lt;width&gt; width1 &lt; /width &gt;
     &lt;height&gt;height1&lt; /height &gt;
     &lt;link&gt;link1&lt; /link &gt;
     &lt;url&gt;url_to_image1&lt; /url &gt;
     &lt; /image &gt;
    &lt;/Book&gt;

&lt;Book ISBN="xxxx"&gt;
&lt;title&gt; Title of book 2&lt;/title&gt;
    &lt;author&gt; Author 2&lt;/author&gt;

     &lt;image&gt;
     &lt;title&gt;Title2&lt; /title &gt;
     &lt;width&gt; width2 &lt; /width &gt;
     &lt;height&gt;height2&lt; /height &gt;
     &lt;link&gt;link2&lt; /link &gt;
     &lt;url&gt;url_to_image2&lt; /url &gt;
     &lt;/image&gt;
    &lt;/Book&gt;

```

\</Books\>

Anyone? Thanks

---

<div class="post-metadata">

### Author: ![Alex\_Lexcuk](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@Alex\_Lexcuk](https://forum.kirupa.com/u/Alex_Lexcuk)
#### Post date: [June 1, 2008, 8:32pm UTC](https://forum.kirupa.com/t/loading-external-xml/261894/2 "2008-06-01T20:32:30Z")

</div>

[http://dnadillo.dn.ua/fla/XML/img-xml.swf](http://dnadillo.dn.ua/fla/XML/img-xml.swf)

---

<div class="post-metadata">

### Author: ![mjg08](https://avatars.discourse-cdn.com/v4/letter/m/b3f665/32.png) [@mjg08](https://forum.kirupa.com/u/mjg08)
#### Post date: [June 2, 2008, 12:44am UTC](https://forum.kirupa.com/t/loading-external-xml/261894/3 "2008-06-02T00:44:48Z")

</div>

I haven’t looked into the tutorial much but as long as your tag is inside the \<book\> tag then you should be able to access it from flash for example for your code I would you use a for loop

```auto

//if you use an array to capture the imageUrl for example
var urlArray:Array = new Array();
xmContent = XML(xmLoader.data);
//then the for loop to look into each <book>
for (var pic:String in xmContent.book){
  
   urlArray[pic] = xmContent.book[pic].image.url;

}

```

and then use that as a URLRequest… I hope this helped

---

<div class="post-metadata">

### Author: ![agabriel](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@agabriel](https://forum.kirupa.com/u/agabriel)
#### Post date: [June 2, 2008, 3:59pm UTC](https://forum.kirupa.com/t/loading-external-xml/261894/4 "2008-06-02T15:59:39Z")

</div>

Adding to what mjg08 wrote:

After accessing your XML data, store your url in an XMLList.

```auto

var xmlContent:XML;
var imageList:XMLList;

imageList = xmlContent.Book[0].image.url;

```

Then from your XMLList create a URLRequest to store that url, use the Loader class to load it, and finally add it to the stage.

```auto

var imageLocation:URLRequest = new URLRequest();
var imageLoader:Loader = new Loader();

imageLoader.load(imageLocation);

addChild(imageLoader);

```

---

<div class="post-metadata">

### Author: ![luka\_luka](https://avatars.discourse-cdn.com/v4/letter/l/71c47a/32.png) [@luka\_luka](https://forum.kirupa.com/u/luka_luka)
#### Post date: [June 2, 2008, 5:12pm UTC](https://forum.kirupa.com/t/loading-external-xml/261894/5 "2008-06-02T17:12:29Z")

</div>

Thank you all for replying! I took a look at Alex Lexcuk’s fla and xml. And it has a lot of usefull data. Thank you Alex!

I have last youestion (I hope so).

This is code of altered alex’s xml (shortened for my needs):

\<Books\>  
\<Story\>  
\<h1\>

```
     &lt;br/&gt;
     &lt;font color="#0099FF"size="9"&gt;GJURO 2&lt;/font&gt;
     &lt;font color="#FFFFFF" size="9"&gt;Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus justo eros, molestie a, porttitor id, blandit ut, tellus. &lt;a href="http://www.gjuro2.hr/"&gt;Check it&lt;/a&gt; &lt;/font&gt;
    
       &lt;br/&gt;

     &lt;br&gt;
 &lt;img src="magna_carta_131.JPG" width="35" height="35" hspace="100" vspace="10"&gt;&lt;/img&gt;
       &lt;/br&gt;

     &lt;/h1&gt;
&lt;/Story&gt;

```

\</Books\>

and when I load it into my flash, it shows this:

which is great! But is it possible to make something like this (sorry for low q):

The difference is about alingment. First comes the picture, then text besides it! And I don’t understand why it’s so much space between Gjuro2 text and rest of text

I would be thankfull for some help.

and this is as code:

> function xmlLoaded(event:Event):void {  
> myXML = XML(myLoader.data);  
> ParseStory(myXML);  
> }  
> // prolazak kroz fju  
> function ParseStory(bookInput:XML):void {  
> var my\_string:String;

```
my_string=bookInput.Story;
remove_enter(my_string);
my_txt.htmlText=remove_enter(my_string);

```

}

var style:StyleSheet = new StyleSheet();  
var styleA:Object = new Object();  
var styleA\_linc:Object = new Object();  
var style\_div:Object = new Object();  
style\_div.color=“#220000”;  
style\_div.textAlign=“left”;  
styleA\_linc.color=“#220000”;  
styleA.color = “#CCCCCC”;  
//styleA\_linc.fontWeight = “bold”;  
[//styleA.fontStyle](https://styleA.fontStyle) = “italic”;  
style.setStyle(“h1”, style\_div);  
[//styleA.textDecoration=](https://styleA.textDecoration=)“underline”;  
style.setStyle(“a”, styleA);  
style.setStyle(“a:hover”, styleA\_linc);

my\_txt.styleSheet=style;

function remove\_enter(str\_line:String):String {  
var my\_array:Array = new Array();  
var my\_array\_more\_null:Array = new Array();  
var my\_enter:String;  
var j:int=0;  
my\_array = str\_line.split("  
");//split on symbol

```
for (var i = 0; i&lt;my_array.length; i++) {
    my_enter=my_array*;
    //my_array*=my_enter.substring(0,my_enter.length);//remove enter form end line
    //trace(my_array*+" "+my_enter.length);//look at result
    if (my_enter.length&gt;1) {
        my_array_more_null[j]= my_enter;
        j++;
    }
}    
my_enter=my_array_more_null.join("");//symbol for upper plus line(default ",")
return my_enter;

```

}

---

<div class="post-metadata">

### Author: ![jwopitz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jwopitz/32/1556_2.png) [@jwopitz](https://forum.kirupa.com/u/jwopitz)
#### Post date: [June 2, 2008, 5:32pm UTC](https://forum.kirupa.com/t/loading-external-xml/261894/6 "2008-06-02T17:32:54Z")

</div>

Just a quick hint for XML:

If you have something like this:

\<Book\>  
\<title\>asdfsdfdsf\</title\>  
\<img\>ashfsadfjsadf.jpg\</img\>  
\<alt\>alt\</alt\>  
\</Book\>

You should using double dot syntax and cast to those expected value types such as

```auto

var xml:XML = //the above XML

var b:Book = new Book();
b.title = String(xml..title[0]);
b.img = String(xml..img[0]);
b.alt = String(xml..alt[0]);

Now for my shameless plug. There is a class in AppCoreLib called ClassUtil that has translation methods so that you can pass a class and an XML object and it will spit out a new instance filled in. Of course it requires that you map the XML to the public vars on the class. Check it out:

http://appcorelib.googlecode.com/svn/trunk/asdoc/index.html
http://code.google.com/p/appcorelib/source/browse/trunk/src/appCoreLib/utils/ClassUtil.as
```

---

<div class="post-metadata">

### Author: ![luka\_luka](https://avatars.discourse-cdn.com/v4/letter/l/71c47a/32.png) [@luka\_luka](https://forum.kirupa.com/u/luka_luka)
#### Post date: [June 2, 2008, 5:55pm UTC](https://forum.kirupa.com/t/loading-external-xml/261894/7 "2008-06-02T17:55:10Z")

</div>

[quote=jwopitz;2334925]  
Now for my shameless plug. There is a class in AppCoreLib called ClassUtil that has translation methods so that you can pass a class and an XML object and it will spit out a new instance filled in. Of course it requires that you map the XML to the public vars on the class. Check it out:

[http://appcorelib.googlecode.com/svn/trunk/asdoc/index.html](http://appcorelib.googlecode.com/svn/trunk/asdoc/index.html)  
[http://code.google.com/p/appcorelib/source/browse/trunk/src/appCoreLib/utils/ClassUtil.as[/quote]](http://code.google.com/p/appcorelib/source/browse/trunk/src/appCoreLib/utils/ClassUtil.as%5B/quote%5D)

Thanx for reply, but I’m not very good in as and xml so ClassUtil means little to me…:jail:

Is there way that I can do what I said in my prevoius post? The problem is that I don’t know how many of this paragraphs will be in my xml, so it has to be in one dynamic text box…Otherwise I would separate different text boxes for different data (images, text etc.). Anyone?

Thanks

Edit:

[quote=jwopitz;2334925]Just a quick hint for XML:

If you have something like this:

\<Book\>  
\<title\>asdfsdfdsf\</title\>  
\<img\>ashfsadfjsadf.jpg\</img\>  
\<alt\>alt\</alt\>  
\</Book\>

You should using double dot syntax and cast to those expected value types such as

```auto

var xml:XML = //the above XML

var b:Book = new Book();
b.title = String(xml..title[0]);
b.img = String(xml..img[0]);
b.alt = String(xml..alt[0]);[/quote]

I could use this part but I don't know how many books there will be, so I have to load them dinamically..Any way of using this but be able to load them dinamically? Probably for loop?
```
