0

I have some XML that needs to be manipulated into a string to render some instructions. The text looks like this

<?xml version="1.0" encoding="UTF-8"?>
<instructions id="detection" version="1.0">
<instruction task="detection">
    <phrase type="header">HAS THE CARD TURNED OVER?<nl/><nl/><nl/></phrase>
    <phrase type="practice">you are now going to do a practice.<nl/><nl/></phrase>
    <phrase type="real">You are now going to do a test.<nl/><nl/></phrase>
    <phrase>As soon as the card turns face up:<nl/><nl/></phrase>
    <phrase><ts/><ts/>Press YES.<nl/><nl/></phrase>
    <phrase>Go as fast as you can and try not to make any mistakes.<nl/><nl/></phrase>
    <phrase>If you press YES before a card turns face up, you will hear an error sound.</phrase>
</instruction>
</instructions>

Now, all I need to do is the following

  1. Replace all <nl/> with \n
  2. Replace all <ts/> with \t
  3. Conditionally select practice or real, probably by removing the other
  4. Remove all XML bits remaining to result in a string.

so lets say I want the practice version of this, I should end up with

HAS THE CARD TURNED OVER?\n\n\n
you are now going to do a practice.\n\n
As soon as the card turns face up:\n\n
\t\tPress YES.\n\n
Go as fast as you can and try not to make any mistakes.\n\n
If you press YES before a card turns face up, you will hear an error sound.

Now, I have the opportunity to change the structure of the XML if the current form isn't ideal for this, but what I'm not sure is if I can do all of the above with e4X or I need to also use regex's? Some examples would be great.

Dale Fraser
  • 4,623
  • 7
  • 39
  • 76
  • Do you actually need "\n", or would a visual break, such as what you get with using
    tags, work?
    – Amy Blankenship Jul 25 '12 at 02:50
  • Its going into a textarea which, it currently uses hard coded text with \n \t and we are moving it to an xml configuration system – Dale Fraser Jul 25 '12 at 02:59
  • I'd use
    instead of trying to replace with \n, then. I think you can just use tabs where you need tabs. At that point, you just need to call toString() on the XMLList containing your real or practice values. If you don't set condenseWhite on your TextArea, you may not even need the
    tags, but could probably just format the text by putting carriage returns where you want them.
    – Amy Blankenship Jul 25 '12 at 03:16
  • I don't understand. E4X is only for manipulating the XML, selecting, adding, removing items, but not for changing the content of it. For that you'll obviously need to use the replace method or regex if you prefer and eventually affect the new value. – poussma Jul 25 '12 at 09:08

1 Answers1

1

It can be done with E4X, probably not as elegantly as regex. Here's an example of replacing <nl> with "\n" using E4x:

package
{
    import flash.display.Sprite;

    public class e4xStuff extends Sprite
    {
        private var srcxml:XML;

        public function e4xStuff()
        {
            srcxml = new XML(   '<instructions id="detection" version="1.0">' +
                '<instruction task="detection">' +
                '<phrase type="header">HAS THE CARD TURNED OVER?<nl/><nl/><nl/></phrase>' +
                '<phrase type="practice">you are now going to do a practice.<nl/><nl/></phrase>' +
                '<phrase type="real">You are now going to do a test.<nl/><nl/></phrase>' +
                '<phrase>As soon as the card turns face up:<nl/><nl/></phrase>' +
                '<phrase><ts/><ts/>Press YES.<nl/><nl/></phrase>' +
                '<phrase>Go as fast as you can and try not to make any mistakes.<nl/><nl/></phrase>' +
                '<phrase>If you press YES before a card turns face up, you will hear an error sound.</phrase>' +
                '</instruction>' +
                '</instructions>');


            processNode(srcxml);
            trace(srcxml);
        }

        private function processNode(xml:XML):XML
        {
            //replace <nl/> with \n
            if(xml.name() == "nl")
            {
                return new XML("\n");
            }

            var children:XMLList = xml.children();
            if(children.length() == 0)
            {
                return xml;
            }

            //remove the children
            xml.setChildren(new XMLList());   

            //put the children back, one-by-one, after checking for <nl/>
            for(var i:int=0; i<children.length(); i++)
            {
                xml.appendChild(processNode(children[i])); 
            }
            return xml;
        }
    }
}

A list of E4X methods is posted at http://wso2.org/project/mashup/0.2/docs/e4xquickstart.html You can check for practice or real using xml.@type

duggulous
  • 2,427
  • 3
  • 23
  • 40