dcsimg
A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Find word searching XML-nodes?

  1. #1
    Member
    Join Date
    Nov 2006
    Posts
    79

    Find word searching XML-nodes?

    Hello, im new here but I hope I can get help fast, cause the deadline is coming up.

    As you can see in this loop, I am going through some nodevalues that is containing ordinary text. What I want is for the loop to stop when it finds a nodevalue NOT containing the words: TV3, SVT, TV4. How should I complete my loop for it to achieve that?


    if (ok == true) {
    for(i=0; i<10; i++){
    element=this.firstChild.firstChild.childNodes[i].childNodes[4].childNodes[0].nodeValue;

    nr=i;
    }

  2. #2
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    PHP Code:
    var keywrds = new Array("TV3""SVT""TV4");//
    var found false;

    var 
    nValue=this.firstChild.firstChild.childNodes[i].childNodes[4].childNodes[0].nodeValue;
    for (var 
    j=0;j<keywrds.length;j++){
        if (
    nValue.indexOfkeywrds[j] != -1){//found
            
    found=true;
            break;
        }
    }
    if (
    found){
        
    trace("hey I found one of the following items: "+keywrds);
    }else{
        
    trace("doh, nothing found");

    but your loop doesn´t make any sense to me so insert yourself
    Last edited by renderhjs; 08-27-2007 at 12:46 PM.

  3. #3
    Member
    Join Date
    Nov 2006
    Posts
    79

    Thanks but..

    Thank you for your reply, but it is not working as it should. Maybe im doing wrong, I dont know. What happens is that I always get the message that it has found one of the keywords, even if it should not. What I want to get out of this loop is the number (i) of the first node ITEM from the XML-file that is not containing one of the keywords. And I want it stored in a variable. This is the actionscript now:

    System.useCodepage = true;
    my_XML = new XML();
    my_XML.ignoreWhite = true;
    my_XML.onLoad = myLoad;
    my_XML.load("kanal5-rss.xml");

    function myLoad(ok) {
    if (ok == true) {
    var keywrds = new Array("TV3", "SVT", "TV4");
    var found = false;
    //I have assigned different values to i here, always with the result that one of the keywords were found
    var nValue=this.firstChild.firstChild.childNodes[i].childNodes[4].childNodes[0].nodeValue;
    trace(nValue);
    for (var j=0;j<keywrds.length;j++){
    if (nValue.indexOf(keywrds[j] != -1)){//found
    found=true;
    break;
    }
    }
    if (found){
    trace("hey I found one of the following items: "+keywrds);
    }else{
    trace("doh, nothing found");
    }


    This is the xml:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <rss xmlns:admin="http://webns.net/mvcb/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
    <channel>
    <title>kanal5.se - roligare TV</title>
    <description>Det senaste från tv-underhållningsvärlden från Kanal 5.</description>
    <link>http://www.kanal5.se</link>

    <item>
    <title>Bland änglar och träsktroll</title>
    <pubDate>2007-08-17 10:30:00.0</pubDate>
    <link>http://www.kanal5.se/web/guest/hem?p_p_id=K5JOURNAL_VIEWER_INSTANCE_xAzU&amp;p_p_ action=0&amp;p_p_state=normal&amp;p_p_mode=view&am p;_K5JOURNAL_VIEWER_INSTANCE_xAzU_struts_action=%2 Fk5journal%2Fview&amp;_K5JOURNAL_VIEWER_INSTANCE_x AzU_groupId=1701&amp;_K5JOURNAL_VIEWER_INSTANCE_xA zU_articleId=19499</link>
    <dc:creator>Alexander Dunerfors</dc:creator>
    <description>Denna vecka sänds fortsättningen på såväl Shrek och Dödligt vapen, men mitt hetaste tips blir ändå sköna Charlies änglar ? utan hämningar.</description>
    <bildurl>http://tekla.ing.umu.se/~el00dsm/bild1.jpg</bildurl>
    <guid isPermaLink="false">@2007-08-17 14:12:11.0</guid>
    </item>

    <item>
    <title>Janice gör allt för djuren</title>
    <pubDate>2007-08-21 15:00:00.0</pubDate>
    <link>http://www.kanal5.se/web/guest/hem?p_p_id=K5JOURNAL_VIEWER_INSTANCE_xAzU&amp;p_p_ action=0&amp;p_p_state=normal&amp;p_p_mode=view&am p;_K5JOURNAL_VIEWER_INSTANCE_xAzU_struts_action=%2 Fk5journal%2Fview&amp;_K5JOURNAL_VIEWER_INSTANCE_x AzU_groupId=1701&amp;_K5JOURNAL_VIEWER_INSTANCE_xA zU_articleId=21383</link>
    <dc:creator>Alexander Dunerfors</dc:creator>
    <description>Hon är van vid en *****ig attityd mot sina medmänniskor, men värnar om djuren. Nyligen kastade Janice Dickinson och hennes modeller kläderna i en naken pälsprotest.</description>
    <guid isPermaLink="false">@2007-08-21 15:32:15.0</guid>
    </item>

    <item>
    <title>K-Fed gästspelar i One Tree Hill</title>
    <pubDate>2007-08-21 11:00:00.0</pubDate>
    <link>http://www.kanal5.se/web/guest/hem?p_p_id=K5JOURNAL_VIEWER_INSTANCE_xAzU&amp;p_p_ action=0&amp;p_p_state=normal&amp;p_p_mode=view&am p;_K5JOURNAL_VIEWER_INSTANCE_xAzU_struts_action=%2 Fk5journal%2Fview&amp;_K5JOURNAL_VIEWER_INSTANCE_x AzU_groupId=1701&amp;_K5JOURNAL_VIEWER_INSTANCE_xA zU_articleId=21074</link>
    <dc:creator>Erika Mollberg</dc:creator>
    <description>Britneys struliga ex-make, Kevin Federline, 29, återupptar sin påbörjade skådiskarriär och hoppar in i ett avsnitt av tonårssåpan One Tree Hill.</description>
    <guid isPermaLink="false">@2007-08-21 11:37:30.0</guid>
    </item>

    <item>
    <title>Emma slår tillbaka|med nytt tv-projekt</title>
    <pubDate>2007-08-21 13:00:00.0</pubDate>
    <link>http://www.kanal5.se/web/guest/hem?p_p_id=K5JOURNAL_VIEWER_INSTANCE_xAzU&amp;p_p_ action=0&amp;p_p_state=normal&amp;p_p_mode=view&am p;_K5JOURNAL_VIEWER_INSTANCE_xAzU_struts_action=%2 Fk5journal%2Fview&amp;_K5JOURNAL_VIEWER_INSTANCE_x AzU_groupId=1701&amp;_K5JOURNAL_VIEWER_INSTANCE_xA zU_articleId=21155</link>
    <dc:creator>Andreas Davidsson</dc:creator>
    <description>Det har spekulerats i att skånepinglan Emma Andersson har varit på väg bort från tv-rutan. Nu slår hon tillbaka med full kraft.</description>
    <guid isPermaLink="false">@2007-08-21 13:21:05.0</guid>
    </item>
    </channel>
    </rss>
    Last edited by svanholm; 08-27-2007 at 06:44 PM.

  4. #4
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    have you checked your xml in a xml validator? - at least opera and firefox tell me that it is full of errors and wont display it. I edited the link values and then it worked. Make sure it´s valid so that flash reads it propper otherwise you won´t be able to read much of it in flash.
    But maybe it was just because this BBoard parsed it different

    anyway what I wrote:
    PHP Code:
    System.useCodepage true;
    my_XML = new XML();
    my_XML.ignoreWhite true;
    my_XML.onLoad myLoad;
    my_XML.load("kanal5-rss.xml");

    function 
    myLoad(ok) {
        if (
    ok) {
            
            var 
    keywrds = new Array("TV3""SVT""TV4");
            var 
    first_false false;//the first node that doesn´t contain the keywords    

            
    var node my_XML.firstChild.firstChild.childNodes;//node to browse through
            
    for (var i=0;i<node.length;i++){

                var 
    found false;//default value
                
    for (var j=0;j<keywrds.length;j++){
                    if (
    node[i].indexOf(keywrds[j] != -1)){//found it´s there
                        
    found=true;
                    }
                }
        
                if (!
    found){
                    
    first_false node[i];//assign current node to the first node that does not contain...
                    
    break;//exit this loop

                
    }
            }

            if (
    first_false != false){//there is 1 first item that doesn´t contain any of the array items
                
    trace("1st node not containing: "+keywrds+" is:\r"+first_false);
            }

        }else{
            
    trace("xml file doesn´t exist!!");
        }

    rss sure sucks,- using title,description and link in the same level as the items- bleh. It browses trhough the nodes of the <channel> node and determines the node that does not contain any of the elements from the array list.

    next time try to use the code or php tags in this forum wich makes your xml or code easier to read

  5. #5
    Member
    Join Date
    Nov 2006
    Posts
    79
    Thank you again for taking time to help me. The code makes more sence to me now when you have commented it. But I still have the same problem. It always tell me that the first node <title>kanal5.se - roligare TV</title> doesnt contain one of the keywords, even if I change a keyword to "roligare". But if you say that the code work for you I have to do something else wrong. When I test it I only publish inside flash and read the trace, is that OK? The trace says:

    1st node not containing: roligare,SVT,TV4 is:
    <title>kanal5.se - roligare TV</title>

  6. #6
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    can you attach a propper XML (that is valid),- like I said perhaps the one you posted in your message as text got scrambled.
    Then I´ll test it myself

  7. #7
    Member
    Join Date
    Nov 2006
    Posts
    79

  8. #8
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    oh well I am sorry my stupid fault (can happen if you write the code just from scratch):
    PHP Code:
    ystem.useCodepage true
    my_XML = new XML(); 
    my_XML.ignoreWhite true
    my_XML.onLoad myLoad
    my_XML.load("kanal5-rss.xml"); 

    function 
    myLoad(ok) { 
        if (
    ok) { 
             
            var 
    keywrds = new Array("TV3""SVT""TV4"); //
            
    var first_false false;//the first node that doesn´t contain the keywords     

            
    var node my_XML.firstChild.firstChild.childNodes;//node to browse through 
            
            
    for (var i=0;i<node.length;i++){ 
                if (
    node[i].nodeName != "description" && node[i].nodeName != "title" && node[i].nodeName != "link"){
                    var 
    found false;//default value 
                    
    for (var j=0;j<keywrds.length;j++){ 
                        if (
    node[i].toString().indexOf(keywrds[j]) != -1){//found it´s there 
                            
    found=true;
                        }
                    } 
                    if (!
    found){ 
                        
    first_false node[i];//assign current node to the first node that does not contain... 
                        
    break;//exit this loop 
        
                    

                }
            } 
            if (
    first_false != false){//there is 1 first item that doesn´t contain any of the array items 
                
    trace("1st node not containing: "+keywrds+" is:\r"+first_false); 
            }else{
                
    trace("all items contain one of the following strings: "+keywrds);
            }
        }else{ 
            
    trace("xml file doesn´t exist!!"); 
        } 

    I added as well the line
    PHP Code:
    if (node[i].nodeName != "description" && node[i].nodeName != "title" && node[i].nodeName != "link"){ 
    not so sure anymore how I fixed it but it was something like
    node[i].toString() returning me the propper string to search through in the inner loop- just node[i] didn´t work, anyway please test it again
    wich will make sure it´s not the description, title or link node because those are propably general information nodes and not the ones that should be checked.

  9. #9
    Member
    Join Date
    Nov 2006
    Posts
    79
    Everything works now! Thank you very much. Though there are two things that I was not clear about. The keywords are only forbidden in "description". It is totaly fine if they are somewhere else. What is it that I should change for that to happen?
    And I would like to save the node number or the first, second and third ITEM not containing the keywords in "description" to three different variables.

    Thank you so much again for taking time to help and educate me.
    /David in Sweden
    Last edited by svanholm; 08-28-2007 at 09:18 AM.

  10. #10
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    fixed:
    PHP Code:
    System.useCodepage true
    my_XML = new XML(); 
    my_XML.ignoreWhite true
    my_XML.onLoad myLoad
    my_XML.load("kanal5-rss.xml"); 

    function 
    myLoad(ok) { 
        if (
    ok) { 
              
            var 
    keywrds = new Array("TV3""SVT""TV4"); // 
            
    var first_false = new Array();//the first nodes that dont contain the keywords      

            
    var node my_XML.firstChild.firstChild.childNodes;//node to browse through 
             
            
    for (var i=0;i<node.length;i++){ 
                if (
    node[i].nodeName != "description" && node[i].nodeName != "title" && node[i].nodeName != "link"){ 
                    
                    
                    var 
    subNode undefined;
                    
                    for (
    k=0;knode[i].childNodes.length;k++){
                        if ( 
    node[i].childNodes[k].nodeName == "description"){
                            
    subNode node[i].childNodes[k];
                        }
                    }
                    
                    if ( 
    subNode != undefined ){
                    
                        var 
    found false;//default value 
                        
    for (var j=0;j<keywrds.length;j++){ //browse through keywords, .eg TV4, SVT TV4,..
                            
    if (subNode.toString().indexOf(keywrds[j]) != -1){//found it´s there 
                                
    found=true
                            } 
                        } 
                        if (!
    found){ 
                            
    first_false.push(node[i]);//push current node to the first_false array
                            
    if ( first_false.length >=3){//max 3 items to add to the array,- afte that leave!
                                
    break;//exit this loop
                            
    }
                        } 
                    }
                    
                    
                } 
            } 
            if (
    first_false.length != 0){//there is 1 first item that doesn´t contain any of the array items 
                
    trace("1st "+first_false.length+"x nodes not containing: "+keywrds+" are:\r"+first_false.join("\n---\n")); 
            }else{ 
                
    trace("all items contain one of the following strings: "+keywrds); 
            } 
        }else{ 
            
    trace("xml file doesn´t exist!!"); 
        } 

    I needed to add
    PHP Code:
    var subNode undefined;
                    
                    for (
    k=0;knode[i].childNodes.length;k++){
                        if ( 
    node[i].childNodes[k].nodeName == "description"){
                            
    subNode node[i].childNodes[k];
                        }
                    }
                    
                    if ( 
    subNode != undefined ){ 
    to let it first find the description node (if available). Sadly flash 5-8 dont have a method built in to easily step through the node names that is why most XML files from flash developers and php or others look different. Flash developers usually prefer attributes over extra nodes.

    anyway should work the way you want

  11. #11
    Member
    Join Date
    Nov 2006
    Posts
    79
    Everything works great now. Thank you so much.

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