2016-11-22 6 views
0

xyflagsの数字をほぼすべて<coord>タグから取得するためのヘルプが必要です。私は<pattern>のタグとrotationの属性を持つものの間のものは必要ありません。私は見つけることができない/それらを返すだろう、正しいXPathの文字列を考え出す。XPathでxmapのcoord属性を取得する

<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

関連する問題