A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Search and Replace XML text

  1. #1
    Member
    Join Date
    Aug 2007
    Posts
    33

    Search and Replace XML text

    ok - so I am loading XML and then trying to search and replace some of the text: see below for code (this is after all the xml load stuff....). It is not effecting the xml after loading?!?!?!? please help.

    function loadTxt(success:Boolean):Void {
    if (success) {

    var allXML:XMLNode = txt.firstChild;

    txtCSS = new TextField.StyleSheet();

    cssURL = "css/style.css";
    txtCSS.load(cssURL);

    txtCSS.onLoad = function(success) {
    if (success) {
    scroller.txt.t.styleSheet = txtCSS; scroller.txt.t.autoSize = "left";
    scroller.txt.t.html = true;
    scroller.txt.t.htmlText = allXML.childNodes[0].firstChild.nodeValue;
    }
    };
    searchAndReplace(scroller.txt.t.htmlText, "1", "2");
    } else {
    trace("XML NOT LOADED");
    }
    }

    function searchAndReplace(holder, searchfor, replacement) {
    temparray = holder.split(searchfor);
    holder = temparray.join(replacement);
    return (holder);
    }

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    If everything else is working, then try adding...
    Code:
    scroller.txt.t.htmlText = searchAndReplace(scroller.txt.t.htmlText, "1", "2");

  3. #3
    Member
    Join Date
    Aug 2007
    Posts
    33

    hmmm - still not working

    ok - but still not working - I am posting my test files....

    Thanks for the help, hope you see the issue
    Attached Files Attached Files

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Sorry, but I've only got Flash MX 2004 and can't open your fla.

    But, thought of something else you might try...the search and replace inside the onLoad function.
    Code:
    function loadTxt(success:Boolean):Void {
    	if (success) {
    		var allXML:XMLNode = txt.firstChild;
    		txtCSS = new TextField.StyleSheet();
    		cssURL = "css/style.css";
    		txtCSS.load(cssURL);
    		txtCSS.onLoad = function(success) {
    			if (success) {
    				scroller.txt.t.styleSheet = txtCSS;
    				scroller.txt.t.autoSize = "left";
    				scroller.txt.t.html = true;
    				scroller.txt.t.htmlText = allXML.childNodes[0].firstChild.nodeValue;
    				scroller.txt.t.htmlText = searchAndReplace(scroller.txt.t.htmlText, "1", "2");
    			}
    		};
    	} else {
    		trace("XML NOT LOADED");
    	}
    }
    Last edited by dawsonk; 06-10-2008 at 07:18 PM.

  5. #5
    Member
    Join Date
    Aug 2007
    Posts
    33

    MX file

    Im getting errors on export as MX but here you go.
    Attached Files Attached Files

  6. #6
    :
    Join Date
    Dec 2002
    Posts
    3,518
    See the edited post above, it appears to work.

  7. #7
    Member
    Join Date
    Aug 2007
    Posts
    33

    Nice work!

    Bingo flammingo --- Now - step #2

    Do you know of a tutorial (or can you help with) that will show me how to cross reference the content.xml with a glossary.xml. Meaning that I will like to load both, then display the content.xml info (like we are doing) then if any glossary term from the glossary.xml is present in the content text it would be hi-lighted and as I roll over the term it will run a function(<a HREF="asfunction:myFunction">call function</a>) that will display the glossary term in a pop-up ( like web sites do!)????

  8. #8
    :
    Join Date
    Dec 2002
    Posts
    3,518

    Maybe you can adapt this to what you are trying to do...

    Code:
    // clear text from text box we put in to fix flash html bug
    scroller.txt.t.text = "";
    // create new xml object and load text.xml
    var txt:XML = new XML();
    txt.ignoreWhite = true;
    // once the xml is loaded fire the loadTxt function
    txt.onLoad = loadTxt;
    txt.load("content.xml");
    function loadTxt(success:Boolean):Void {
    	if (success) {
    		// get our xml start point
    		var allXML:XMLNode = txt.firstChild;
    		// create new stylesheet
    		txtCSS = new TextField.StyleSheet();
    		// loading our css
    		cssURL = "css/style.css";
    		txtCSS.load(cssURL);
    		// once loaded ..
    		txtCSS.onLoad = function(success) {
    			if (success) {
    				// apply style sheet to description text
    				scroller.txt.t.styleSheet = txtCSS;
    				// formmat text field for html
    				scroller.txt.t.autoSize = "left";
    				scroller.txt.t.html = true;
    				// fill our text field with our xml - everything between the <text> tags 
    				scroller.txt.t.htmlText = allXML.childNodes[0].firstChild.nodeValue;
    				gloss.load("gloss.xml");
    			}
    		};
    	} else {
    		trace("XML NOT LOADED");
    	}
    }
    function searchAndReplace(holder, searchfor, replacement) {
    	temparray = holder.split(searchfor);
    	holder = temparray.join(replacement);
    	return (holder);
    }
    //
    var gloss:XML = new XML();
    gloss.ignoreWhite = true;
    gloss.onLoad = loadGloss;
    function loadGloss(success:Boolean):Void {
    	if (success) {
    		var allXML:XMLNode = gloss.firstChild;
    		var numTerms = allXML.childNodes.length;
    		for (i = 0; i < numTerms; i++) {
    			var myTerm = allXML.childNodes[i].attributes.term;
    			scroller.txt.t.htmlText = searchAndReplace(scroller.txt.t.htmlText, myTerm, "<a HREF='asfunction:myFunction'>" + myTerm + "</a>");
    		}
    	}
    }
    sample used for gloss.xml
    PHP Code:
    <?xml version="1.0"?>
    <content>    
        
    <def term = "page">
    <![CDATA[<span class='title'>Page</span><br/>about the page.]]>
    </def>
    <def term = "number">
    <![CDATA[<span class='title'>Number</span><br/>about the number.]]>
    </def>

    </content>

  9. #9
    Member
    Join Date
    Aug 2007
    Posts
    33

    Yes... I wrote something similar this morning, but....

    Yours is working better....!

    Now I need to finish the myFunction() part: Im getting it to pop the terms.swf but its not loading the gloss.xml info....is there a better way to have the term in gloss.xml popup in a bubble????

    "<a HREF='asfunction:myFunction,terms.swf'>" + myTerm + "</a>"

    function myFunction(movie) {
    gloss.onEnterFrame = loadTerm;
    loadMovieNum(movie, 1);
    }


    BIG THANKS FOR ALL YOUR HELP!

  10. #10
    :
    Join Date
    Dec 2002
    Posts
    3,518
    It depends upon what needs to be displayed for each term. If it's just going to be text, then it's problably easier to do something like a toolltip.

    The simplified approach below uses a movieclip, instance name: bubble. With an dynamic html text field, instance name: txt and a movie clip (button), instance name: btnClose.

    Code:
    var gloss:XML = new XML();
    gloss.ignoreWhite = true;
    gloss.onLoad = loadGloss;
    var myGloss = new Array();
    function loadGloss(success:Boolean):Void {
    	if (success) {
    		var allXML:XMLNode = gloss.firstChild;
    		var numTerms = allXML.childNodes.length;
    		for (i = 0; i < numTerms; i++) {
    			var myTerm = allXML.childNodes[i].attributes.term;
    			myGloss[myTerm] = allXML.childNodes[i].firstChild.nodeValue;
    			scroller.txt.t.htmlText = searchAndReplace(scroller.txt.t.htmlText, myTerm, "<a href=\"asfunction:_root.myFunction," + myTerm + "\">" + myTerm + "</a>");
    		}
    	}
    }
    bubble._visible = false;
    function myFunction(term) {
    	bubble._x = _xmouse + 10;
    	bubble._y = _ymouse + 10;
    	bubble.txt.styleSheet = txtCSS
    	bubble.txt.autoSize = true;
    	bubble.txt.htmlText = myGloss[term];
    	bubble._visible = true;
    	bubble.btnClose.onPress = function(){
    		this._parent._visible = false;
    	}
    }

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