2016-04-26 89 views
0

我是從一個API retreiving XML和使用PHP將其拖放到一個不錯的有序陣列..沒有返回的XML兒童PHP

基本上它是一類菜單,返回類別會去的3個級別進入一個三層項目列表。

第一層有一個節點名稱爲'CustomCategory',在這個節點中有一個名爲'ChildCategory'的子節點。這是完美的,我可以在第一個循環內並回顯第二個等級。

問題在於,在第二級內,第三級節點也被稱爲「ChildCategory」。

它看起來有點像這樣:

<CustomCategories> 
    <CustomCategory> 
      <Name>Category 1</Name> 
      <ChildCategory> 
       <Name>Child 1</Name> 
       <ChildCategory> 
        <Name>Child 1a</Name> 
       </ChildCategory> 
      <ChildCategory> 
      <Name>Child 2</Name> 
      </ChildCategory> 
    </CustomCategory> 
    <CustomCategory> 
      <Name>Category 2</Name> 
    </CustomCategory> 
</CustomCategories> 

這意味着,當我循環通過第一級要求獲得孩子們,這不只是在看第二個層次,它會通過每個第三級,因爲它是相同的節點名稱...

如何運行我的第二個循環,但忽略孩子?

這是我的時刻代碼:

$responseDoc = new DomDocument(); 
$responseDoc->loadXML($responseXml); 

$categories=[]; 

// loop level 1 
foreach ($responseDoc->getElementsByTagName('CustomCategory') as $cat1) { 

      $Name1 = $cat1->getElementsByTagName('Name')->item(0)->nodeValue; 
      $cats1['name1'] = $Name1; 

      // loop level 2 
      foreach ($cat1->getElementsByTagName('ChildCategory') as $cat2) { 

       $Name2 = $cat2->getElementsByTagName('Name')->item(0)->nodeValue; 
       $cats2['name2'] = $Name2; 
       $cats1['subcat']= $cats2; 

      } 

      $categories[] = $cats1; 

} 

注:這是一個有點剝離下來,因爲我做的陣列是如何被責令他們在去一些時髦的東西

在當我回應這一點的時候,它創建了一個列表,所有第二/第三級別合在一起。

我要出去把數據到底是這樣的:

我需要在名稱中的值,這樣我就可以把它們放到一個數組和回聲出如:

<ul> 
    <li>Category 1 
     <ul> 
      <li>Child1 
       <ul> 
        <li>Child 1a</li> 
       </ul> 
      </li> 
      <li>Child 2</li> 
     </ul> 
    </li> 
    <li>Category 1</li> 
</ul> 

但是第三層的孩子在第二圈出來......

非常感謝!

+1

提供一個更主要的''內部節點''&別說你需要得到什麼樣的價值觀。給定的XML無效。 –

+0

它看起來對我有效(儘管我在最後一條線上留下了一條鬆弛的線......現在我將添加這條線,爲什麼還需要第三條線?我可以添加這個,但是..? 我尋找的值我會加上質疑.... – zombiecode

回答

0

DOMNode::getElementsByTagName()返回具有該名稱的所有後代元素節點。您可以迭代$childNodes屬性並驗證每個節點類型和名稱,或者使用Xpath。

$document = new DOMDocument(); 
$document->loadXml($xml); 
$xpath = new DOMXpath($document); 

foreach ($xpath->evaluate('/CustomCategories/CustomCategory') as $customCategory) { 
    echo $xpath->evaluate('string(Name)', $customCategory), "\n"; 
    foreach ($xpath->evaluate('ChildCategory', $customCategory) as $childCategory) { 
    echo ' -> ', $xpath->evaluate('string(Name)', $childCategory), "\n"; 
    } 
} 

輸出:

Category 1 
-> Child 1 
-> Child 2 
Category 2 

你提到,你需要訂購的類別。一個簡單的方法是將節點列表轉換爲數組並使用自定義排序。

$document = new DOMDocument(); 
$document->loadXml($xml); 
$xpath = new DOMXpath($document); 

$customCategories = iterator_to_array(
    $xpath->evaluate('/CustomCategories/CustomCategory') 
); 
// sort the categories by name, descending 
uasort(
    $customCategories, 
    function($one, $two) use ($xpath) { 
    return -1 * strcasecmp(
     $xpath->evaluate('string(Name)', $one), 
     $xpath->evaluate('string(Name)', $two) 
    ); 
    } 
); 

foreach ($customCategories as $customCategory) { 
    echo $xpath->evaluate('string(Name)', $customCategory), "\n"; 
    foreach ($xpath->evaluate('ChildCategory', $customCategory) as $childCategory) { 
    echo ' -> ', $xpath->evaluate('string(Name)', $childCategory), "\n"; 
    } 
} 

輸出:

Category 2 
Category 1 
-> Child 1 
-> Child 2