2016-11-22 146 views
0

我需要幫助獲取幾乎所有<coord>標籤所示的所有x,yflags數字。我不需要<pattern>標籤和具有rotation屬性的標籤。我似乎無法找到/想出將返回這些的正確的XPath字符串。使用XPath獲取xmap座標屬性

<parts count="1" current="0"> 
      <part name="default part"> 
       <objects count="37"> 
        <object type="1" symbol="166"> 
        <coords count="26"> 
         <coord x="-13110" y="-20755" flags="1"/> 
         <coord x="-13360" y="-20705"/> 
         <coord x="-13680" y="-20615"/> 
         <coord x="-13610" y="-20375" flags="18"/> 
        </coords> 
        <pattern rotation="0"> 
         <coord x="0" y="0"/> 
        </pattern> 
       </object> 
       <object type="0" symbol="170" rotation="0"> 
        <coords count="1"> 
         <coord x="-13770" y="-20815"/> 
        </coords> 
       </object> 
       <object type="1" symbol="157"> 
        <coords count="13"> 
         <coord x="-13195" y="-27090" flags="1"/> 
         <coord x="-13415" y="-25930"/> 
         <coord x="-13360" y="-25125"/> 
        </coords> 
        <pattern rotation="0"> 
         <coord x="0" y="0"/> 
        </pattern> 
       </object> 

所以我期望的輸出是某事像這樣:

-13110 -20755 1 
-13360 -20705 
-13680 -20615 
-13610 -20375 18 

-13195 -27090 1 
-13415 -25930 
-13360 -25125 

它需要在JavaScript。這裏是full map

回答

1

<?php 

class Part{ 
    public $coords; 
    function __construct(){ 
     $this->coords = array(); 
    } 
    function addCoordinate(Coord $coord){ 
     $this->coords[] = $coord; 
    } 
} 

class Coord{ 
    public $x; 
    public $y; 
    public $flags; 
} 

class XMLCoordsParser{ 
    function parse($xmlString){ 
     $doc = new DOMDocument(); 
     $doc->loadHTML($xmlString); 

     $xpath = new DOMXPath($doc); 

     //get all part objects and for each check if the boolean representation of its roatation attribute value is equal to 0 (false), which means it doesn't contain that attribute. 
     $result = $xpath->query("//parts/part/objects/object[boolean(@rotation)=0]/coords"); 

     $allParts = array(); 

     for($i=0; $i<$result->length; $i++){ 
      $newPart = new Part(); 

      // using the "./" means we start under the given node, which is <coords> tag 
      $coordsNodeList = $xpath->query("./coord", $result->item($i)); 

      for($j=0; $j<$coordsNodeList->length; $j++){ 
       $newCoord = new Coord(); 
       $coordNodeAttributes = $coordsNodeList->item($j)->attributes; 
       if($x = $coordNodeAttributes->getNamedItem("x")){ 
        $newCoord->x = $x->nodeValue; 
       } 
       if($y = $coordNodeAttributes->getNamedItem("y")){ 
        $newCoord->y = $y->nodeValue; 
       } 
       if($flags = $coordNodeAttributes->getNamedItem("flags")){ 
        $newCoord->flags = $flags->nodeValue; 
       } 
       $newPart->addCoordinate($newCoord); 
      } 
      $allParts[] = $newPart; 
     } 
     return $allParts; 
    } 
} 

libxml_use_internal_errors(true); 

$xmlString = file_get_contents("data2.xml"); 

$allParts = (new XMLCoordsParser())->parse($xmlString); 

var_dump($allParts); 

?> 
+0

謝謝!我解析成javascript後,它的工作原理! – Matis