gpt4 book ai didi

javascript - 使用 XPath 获取 xmap 坐标属性

转载 作者:行者123 更新时间:2023-12-03 05:35:19 25 4
gpt4 key购买 nike

我需要帮助来获取所有 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

最佳答案

这里是

<?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);

?>

关于javascript - 使用 XPath 获取 xmap 坐标属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40753674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com