2011-01-11 88 views
0

比方说,我有这个XML软硬度:获取文本节点的唯一路径从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 Recipe</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> 

我需要有一个结构,它像这样

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

我认为,必须用递归函数完成。请帮助。

回答

0

试试这个:

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; 
} 

然后调用trace(parseXML (myXML));

编辑:我在我的第一个答案有点快,这应能正常工作。