开发者

Flex: Getting unique path of text nodes from an XML

Let's say I've this XML

<recipes>
  <pudate>2011-11-01</pubdate>
  <totalNumberOfResults>6</totalNumberOfResults>
  <recipe>
    <title>Ham And Cheese Omlet Roll Recipe</title>
    <href>http://www.grouprecipes.com/38722/ham-and-cheese-omlet-roll.html</href>
    <ingredients>cheddar cheese, dijon mustard, eggs, flour, milk, cream cheese, salt, green onion</ingredients>
  </recipe>
  <recipe>
    <title>Egg Noodle Omlet Recipe</title>
    <href>http://www.grouprecipes.com/63652/egg-noodle-omlet.html</href>
    <ingredients>bacon, cheese, eggs, noodles, onions</ingredients>
  </recipe>
  <recipe>
    <title>Sea Food Omlet R开发者_高级运维ecipe</title>
    <href>http://www.grouprecipes.com/8941/sea-food-omlet.html</href>
    <ingredients>butter, crab meat, green onion, cheese, salt, capers</ingredients>
  </recipe>
  <recipe>
    <title>French Fry - Tater Tot Omlet Recipe</title>
    <href>http://www.grouprecipes.com/20924/french-fry---tater-tot-omlet.html</href>
    <ingredients>eggs, french fries, salt, butter</ingredients>
  </recipe>
</recipes>

I need to have a structure out of it like this

/recipes/pubdate
/recipes/totalNumbeOfResults
/recipes/recipe/title
/recipes/recipe/href
/recipes/recipe/ingredients

I think it must be done using a recursive function. Kindly help.


Try this:

private function parseXML ( xml : XML ) : String 
{

    var path : String = "";

    if (xml.nodeKind( ) == "text") 
    {
        var parent : XML = xml.parent( );
        while (parent != null) 
        {
            path = "/" + parent.name( ) + path;
            parent = parent.parent( );
        }
        path += "\n";
        return path;
    }

    for each ( var child:XML in xml.children( )) 
    {
        path += parseXML( child );
    }

    return path;
}

Then call trace(parseXML (myXML));

Edit: I was a little quick on my first answer, this should work correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜