Question

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.

Était-ce utile?

La solution

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top