使用XPath获取xmap coord属性

时间:2016-11-22 23:41:49

标签: javascript xpath

我需要一个帮助,以便从下面显示的几乎所有x标记中获取所有yflags<coord>个数字。我不需要<pattern>代码与rotation属性代码之间的代码。我似乎找不到合适的XPath字符串来返回那些。

<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 个答案:

答案 0 :(得分: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);

?>