如何访问这个 xml 字段?

How to access to this xml field?

当我 var_dump 这个:

while($row = $updDate->fetch()){//GET FEEDS WHICH UPDATED DATE IS NOT IN DB
        var_dump($this->soccer->GetAllOddsByFixtureMatchId(
        array("fixtureMatch_Id"=>$row['FixtureMatch_Id'])));
        echo $row['FixtureMatch_Id'];}

它 returns 我这个提要。

object(SimpleXMLElement)[11]
public 'OddsList' => 
object(SimpleXMLElement)[12]
  public 'Odds' => 
    array (size=37)
      0 => 
        object(SimpleXMLElement)[13]
          ...
      1 => 
        object(SimpleXMLElement)[14]
          ...
      2 => 
        object(SimpleXMLElement)[15]
          ...
      3 => 
        object(SimpleXMLElement)[16]
          ...
      4 => 
        object(SimpleXMLElement)[17]
          ...
      5 => 
        object(SimpleXMLElement)[18]
          ...
      6 => 
        object(SimpleXMLElement)[19]
          ...
object(SimpleXMLElement)[11]
public 'OddsList' => 
object(SimpleXMLElement)[12]
  public 'Odds' => 
    array (size=37)
      0 => 
        object(SimpleXMLElement)[49]
          ...
      1 => 
        object(SimpleXMLElement)[48]
          ...
      2 => 
        object(SimpleXMLElement)[47]
          ...
      3 => 
        object(SimpleXMLElement)[46]
          ...
      4 => 
        object(SimpleXMLElement)[45]
          ...
      5 => 
        object(SimpleXMLElement)[44]
          ...
      6 => 
        object(SimpleXMLElement)[43]
          ...
object(SimpleXMLElement)[11]
public 'OddsList' => 
object(SimpleXMLElement)[12]
  public 'Odds' => 
    array (size=37)
      0 => 
        object(SimpleXMLElement)[13]
          ...
      1 => 
        object(SimpleXMLElement)[14]
          ...
      2 => 
        object(SimpleXMLElement)[15]
          ...
      3 => 
        object(SimpleXMLElement)[16]
          ...
      4 => 
        object(SimpleXMLElement)[17]
          ...
      5 => 
        object(SimpleXMLElement)[18]
          ...
      6 => 
        object(SimpleXMLElement)[19]
          ...

我想访问 Odds 对象并从中获取一些列并与我的数据库中的列进行比较...

当我使用时效果很好:

->OddsList->Odds

,但在某些情况下我需要 xpath...所以我尝试添加此 ->xpath('/OddsList/Odds')),但它 returns 我为空...

XPath 与 XML 相悖,因此如果看不到 XML,就很难说出确切的问题和解决方案。我猜这个问题是由于 默认命名空间 的存在造成的。考虑以下示例 XML :

<OddsList xmlns="xmlsoccer.com/">
    <Odd>1</Odd> 
    <Odd>2</Odd> 
</OddsList>

上面的 XML 示例中有一个指向地址 "xmlsoccer.com" 的默认命名空间。请注意,后代元素隐式继承祖先默认命名空间,除非另有说明(使用显式命名空间前缀,或使本地默认命名空间指向不同的 URI)。在这种情况下,您需要注册前缀到命名空间URI 的映射,并在XPath 中正确使用注册的前缀。例如:

$string = <<<XML
<OddsList xmlns="xmlsoccer.com/">
    <Odd>1</Odd> 
    <Odd>2</Odd> 
</OddsList>
XML;
$xml = new SimpleXMLElement($string);
$xml->registerXPathNamespace("d","xmlsoccer.com/");
foreach($xml->xpath("/d:OddsList/d:Odd") as $node)
{
    echo $node ."<br>";
}

Demo

输出:

1
2