开发者

Get tag content

开发者 https://www.devze.com 2023-02-27 07:17 出处:网络
I have the following xml struc开发者_StackOverflow社区ture <description> <category id=\"HBLMENEURSPALGA\" order=\"83500\">Spa. Handball Liga Asobal

I have the following xml struc开发者_StackOverflow社区ture

<description>
    <category id="HBLMENEURSPALGA" order="83500">Spa. Handball Liga Asobal
    </category>
    AMAYA Sport San Antonio - C.BM.Torrevieja
</description>  

And with the following code I get

Spa. Handball Liga Asobal AMAYA Sport San Antonio - C.BM.Torrevieja

but I want just this:

AMAYA Sport San Antonio - C.BM.Torrevieja

$teams  =   $game->getElementsByTagName("description");
               foreach ($teams as $team)
               {
                     $info      =   $team->nodeValue;
               }


You need to point to the right node. This node you're pointing to is actually the whole description node, while you need to point to the (implicit) text node containing the string you want.

Solution (provided the string always comes last):

$teams = $xml->getElementsByTagName("description");
foreach ($teams as $team)
{
    $info = $team->lastChild->nodeValue;
    echo "info: $info\n";
}


You need to loop the childNodes of the $team and check if the nodeType == XML_TEXT_NODE

foreach ($teams as $team)
  {
    foreach($team->childNodes as $child)
      {
         if ($child->nodeType == XML_TEXT_NODE && trim($child->nodeValue) != '')
           {
             $info = $child->nodeValue;
             break;
           }
      }
  }

this is beacuse text is also a node (thus the need to loop the childNodes), that has a nodeType value of XML_TEXT_NODE (3)

I am also checking the nodeValue to be non-empty because white-space between nodes can also be treated as textNodes..

0

精彩评论

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