1

我正在寻找 DAYS 的答案!我还没有找到任何.. :( 这是我的 XML 示例:

    <?xml version="1.0" encoding="UTF-8"?>
    <Businesses>
    <Business NAME="Bus1">
    <DEAL ID="2">
    <title>My first Title</Title>
</DEAL>
    <DEAL ID="4">
    <title>My Second Title</Title>
</DEAL>
    </Business>
    <Business NAME="Bus2">
    <DEAL ID="2">
    <title>My 2nd Title</Title>
</DEAL>
    <DEAL ID="5">
    <title>My last Title</Title>
</DEAL>
    </Business>
    <Businesses>

我怎样才能逐个解析这个xml?这是我的代码:

<?php 
  $doc = new domDocument();
  $doc->load("MyXML.xml"); //(The XML Above)
    $Businesses = $doc->documentElement;
    $ebus = $Businesses->childNodes;
    foreach ($ebus as $business) {

     if ($business instanceof domElement) {
     $busname=$business->getAttribute('NAME');
    $shelves = $Businesses->getElementsByTagName("DEAL"); //HERE IS THE PROBLEM

  foreach ($shelves as $deal) {

//Here i have all of the Businesses (I need to present only the first "NAME='Bus1'") for the first iteration! and so on..
}}}
?>

问题是“getElementsByTagName”带来了所有“DEAL”节点,尽管我在第一个业务的 foreach 中!

我试过: $shelves = $Businesses->childNodes; // 但它不正确

感谢您的帮助!!

4

1 回答 1

2

您正在调用它,$Businesses它是documentElement. 您在一开始就分配了它:

$Businesses = $doc->documentElement;

试试这个:

$shelves = $business->getElementsByTagName("DEAL");
于 2012-07-21T14:02:50.443 回答