A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: Display only some XML nodes

  1. #1
    Junior Member
    Join Date
    May 2007
    Posts
    6

    Display only some XML nodes

    Hi,
    Apologies if this has been answered before, but I've searched the forum and can't find an answer that seems to work. I'm pretty new to displaying xml content in Flash, so I may be missing some pretty basic stuff.

    I'm reading a remote xml file and using an external css file to style it; all of that is working fine. Unfortunately, the xml data is used to create hotspots on a map as well as display the text on a text page, so there are nodes that are only used for the map (i.e., latitude and longitude) that I don't want to display on the text page. But I can't figure out how to get tell the actionscript that loads the xml to ignore some of the nodes.

    It has occurred to me that the xml I'm calling may not be formatted correctly for this, but I don't have access to that file, so I have to make do with it as is.

    Anyway, here's the code I'm using to display the xml as text:
    (i've replaced the url to the xml because it's my client's site)

    //init TextArea component
    myText.html = true;
    myText.wordWrap = true;
    myText.multiline = true;
    myText.label.condenseWhite = false;
    contentStyle = new TextField.StyleSheet();
    contentStyle.load("ksm.css");
    myText.styleSheet = contentStyle;
    //load in XML
    ksmContent = new XML();
    ksmContent.ignoreWhite = false;
    ksmContent.load("http://www.site.com/name.xml");
    ksmContent.onLoad = function(success) {
    if (success) {
    myText.text = ksmContent;
    }
    };

    and for completeness sake, here's a sample of the xml in question:

    <events>
    <event>
    <id>24</id>
    <timestamp>1156996800</timestamp>
    <latitude>-29.85</latitude>
    <longitude>31.0167</longitude>
    <title><![CDATA[Rise of Super Tuberculosis]]></title>
    <image/>
    <contenttext><![CDATA[KWAZULU-NATAL, South Africa, Aug. 31, 2006 - A new strain of extensively drug resistant tuberculosis (XDR-TB) has been reported in KwaZulu-Natal. This &quot;super tuberculosis&quot; is resistant to all the first and second line TB drugs, making it virtually untreatable. Additionally, this strain seems to kill particularly rapidly; many patients dying within a month of diagnosis.<br /><br />XDR-TB develops when patients do not take antibiotics properly and can be spread through sneezing and coughing. According to a survey by the CDC and WHO, approximately 2 percent of all TB isolates examined between 2000 and 2004 were XDR-TB.]]></contenttext>
    </event>
    <event>

    The only nodes I want to display are <title> and <contenttext>.

    Oh, and while we're at it, the html tags in the <contenttext> node aren't working properly...

    If anyone can help... tia!!

  2. #2
    Ryan Thomson EvolveDesigns's Avatar
    Join Date
    Oct 2001
    Location
    British Columbia
    Posts
    3,338
    look over the flash help files for the various xml names, then start playing around with creating and tracing a variable path like so:

    if (success) {
    myText.text = ksmContent;
    thisNode = ksmContent.firstChild.firstChild;
    trace(thisNode);

    what i have may not work, finding paths in xml is tricky for me and i find the best method is trial and error in the early stages. If i had to guess i would say that this would trace everything between <id> and </contenttext> so if you added another firstChild it would go one level deeper into your xml. Look up:

    firstChild
    childNodes
    nodeValue
    attribute

    and you should be good to go!

    good luck
    }
    Evolve Designs Interactive Media
    the natural selection

  3. #3
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Set ksmContent.ignoreWhite = false; to true. Otherwise all the white space is counted as nodes.
    - The right of the People to create Flash movies shall not be infringed. -

  4. #4
    Junior Member
    Join Date
    May 2007
    Posts
    6
    Thanks, folk, that's been very helpful... at least some progress is being made. Still in the woods, though... using the two comments above, i've able to get a list of the <id> tags in my xml (it outputs a list of all of them, starting with the "24" you see in the xml i posted, and going all the way up to the "45" in the full xml file). But if I add more firstChild's into the string, i only get a list of "null"s or "undefined"s, not the other tags' contents, like "timestamp", etc.

    And even assuming I'm able to get to the point of being able to identify all the nodes through ActionScript, that still doesn't tell me how to do what I really need, which is how to hide some of them in the output (and i did read through the Flash help on all the subjects listed above.) I thought I'd be able to use removeNode for that, since I can now identify the <id> tag at least, but that doesn't seem to be working.. no errors, but the text still displays...

    Anyone have any ideas?

    thx.

  5. #5
    Ryan Thomson EvolveDesigns's Avatar
    Join Date
    Oct 2001
    Location
    British Columbia
    Posts
    3,338
    well basically xml is actually an array so if you can trace out everything between <events> and </events> by tracing thisNode (meaning ksmContent.firstChild.firstChild from my example) then if you were to trace thisNode[1] it would trace the first element in the array of nodes meaning <id>24</id> and if you traced thisNode[2] it would show <timestamp>1156996800</timestamp> and so on. It's a bit tricky figuring out the paths, but once you get one the rest are a cakewalk..

    If i get a chance later tonight I'll send you a working script for bringing up your nodes (to celebrate my 1000th post! wootwoot)
    Last edited by EvolveDesigns; 05-08-2007 at 06:33 PM.
    Evolve Designs Interactive Media
    the natural selection

  6. #6
    Junior Member
    Join Date
    May 2007
    Posts
    6
    awesome! now we're getting somewhere... i actually had to change the code slightly to
    thisNode = ksmContent.firstChild.firstChild.childNodes;
    to get it to trace out the nodes properly, but now i have a list of all the nodes in the array, and i've commented them so i know which are which.

    i still can't figure out how to use that knowledge to prevent some nodes from displaying , though... (sorry for my denseness); and i also can't figure out how to get the html codes in the text blocks of the <contenttext> nodes to display properly...

    any clue?

    thanks for all your help!

  7. #7
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Instead of firstChild.firstChild try firstChild.nextSibling
    It's a big family
    - The right of the People to create Flash movies shall not be infringed. -

  8. #8
    Junior Member
    Join Date
    May 2007
    Posts
    6
    ahhh, never mind on the first part of that question... I just realized I can do that with the "display" property in the external css file I'm using.

  9. #9
    Junior Member
    Join Date
    May 2007
    Posts
    6
    oops, wrong again... although setting "display" to "none" in the css file successfully hides the tags in question, it leaves space for them, even though the css spec says it shouldn't... so i'm ending up with big white spaces between the paragraphs of text. so i'm back to hoping this is something that can be controlled by Flash...

  10. #10
    Ryan Thomson EvolveDesigns's Avatar
    Join Date
    Oct 2001
    Location
    British Columbia
    Posts
    3,338
    thes reason you can see the tags is because you have to define the textfield as html when loading stuff so

    myText.text = ksmContent;

    would become

    myText.htmlText = ksmContent;
    Evolve Designs Interactive Media
    the natural selection

  11. #11
    Ryan Thomson EvolveDesigns's Avatar
    Join Date
    Oct 2001
    Location
    British Columbia
    Posts
    3,338
    Ok I ended up making a little code that reads the newest digg from digg.com and traces it out, I think it may help you understand how this works


    Code:
    xmlData = new XML();
    xmlData.ignoreWhite = true;
    xmlData.onLoad = loadXML;
    xmlData.load("http://digg.com/rss/index.xml");
    
    function loadXML(success) {
    	if (success) {
    		parseXML();
    	}
    	else {
    		trace("Oh noes! Digg is dead!");
    	}
    }
    
    function parseXML(){
    	story = xmlData.firstChild.firstChild.childNodes;
    	titles = story[4].childNodes[0].childNodes;
    	link = story[4].childNodes[1].childNodes;
    	description = story[4].childNodes[2].childNodes;
    	pubDate = story[4].childNodes[3].childNodes;
    	digCount = story[4].childNodes[5].childNodes;
    	category = story[4].childNodes[7].childNodes;
    	commentsCount = story[4].childNodes[8].childNodes;
    	newestDigg = "<b><a href='" + link + "'>" + titles + "</a></b>\r\r" + description + "\r\r" + pubDate + " :: " + "digg count:" + digCount + "\rcategory: " + category + " :: " + "comments count: " + commentsCount;
    	trace(newestDigg);
    	digg_txt.htmlText = newestDigg;
    }
    I found the path in the xml to all the <items></items> which are the containers for each of the stories, then named that 'story' in the above code. I then dug into each story to separate all the various elements, note the number beside the first childNodes, each one represents one of the elements in the story array. If i wanted to view a different story all together i would change all the 4's beside story to say 7 then it would trace out the the info from a different <item> node.

    I think if you check out the xml im calling on digg beside the above code you should get the idea. (or jsut be totally confused by all this information lol)

    good luck!
    Evolve Designs Interactive Media
    the natural selection

  12. #12
    Junior Member
    Join Date
    May 2007
    Posts
    6
    okay, better all the time! (can't thank you enough for all this help).. the htmlText thing still isn't working though; on both your script and mine, if I change the .text to .htmlText the textarea stays blank. I have it set to "html=true" in the component parameters, but it will only display the output if I have the content set to .text.

    Also, this is working great for displaying a title and text , but the xml I'm pulling from has a whole series of titles and text blocks, and I won't always know how many of them there are. Is there a way to construct a "for" loop or something to address that?

    thanks again...

  13. #13
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    For the textarea use only .text. Only for textFields you need to use htmlText.
    - The right of the People to create Flash movies shall not be infringed. -

  14. #14
    Ryan Thomson EvolveDesigns's Avatar
    Join Date
    Oct 2001
    Location
    British Columbia
    Posts
    3,338
    o right, it's a text area oops

    (with mine, it works you just have to set the textfield to html in the properties bar
    Evolve Designs Interactive Media
    the natural selection

  15. #15
    Ryan Thomson EvolveDesigns's Avatar
    Join Date
    Oct 2001
    Location
    British Columbia
    Posts
    3,338
    the for loop would look something like this:

    for(i=0;i<story.length;i++){
    titles = story[i].childNodes[0].childNodes;
    trace(titles);
    }

    if you added this to the bottom of my parseXML function it would trace out every title for all the new posts at digg.com. If you replaced the trace with an array it would load each title into the array where you could have full control dynamically. hope that helps
    Evolve Designs Interactive Media
    the natural selection

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center