Frage

When I convert XML to a string it converts empty nodes to a singleton leaf. Is there a way to request that it use closing tags instead of singletons?

For example, here is pseudo ActionScript3:

var xml:XML = new XML("<XML><NODE/></XML>");

var output:String = xml.toXMLString();

trace(output);

<XML>
   <NODE/>
</XML>

What I want is:

<XML>
  <NODE></NODE>
</XML>

PS I'm using AS3 and Flash Player 12 which uses E4X.

Back Story:
I'm generating HTML page and then running it through an XML validator and I would like it to be valid and it has problems with things like <link> that in boiler plate they don't close the tag, no "/". There are other cases I have div that when exporting to XML string the XML class rewrites those as <div/> which breaks the HTML page. Sometimes, the nodes will be empty. If I put a space or value in the node it will change the layout of the page.

So basically, there are a few reasons where keeping empty nodes from being written as singletons would be beneficial.

War es hilfreich?

Lösung 2

It appears it is not possible. It may require an update from the Flash Player team.

Andere Tipps

Use an override method to redefine toXMLString:

override public function toXMLString():String
{
    if (!localIndex)
    {
        return source.toXMLString();
    }
    else
    {
        var str:String = "";

        for (var i:int = 0; i < localIndex.length; i++)
        {
            if (i > 0)
                str += "\n"; // this matches how XML works

            str += localIndex[i].toXMLString();
        }

        return str.replace("<NODE/>","<NODE></NODE>");
    }
}

Or, a more generic version:

return str.replace(/<([^\/]+)\/>$/,"<$1></$1>")

References

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top