【发布时间】:2014-03-09 21:27:43
【问题描述】:
我正在使用 PHP 中的 SimpleXMLElement 解析 SVG 文件。 SVG 文件是在 Adobe Illustrator 中精心构建的,遵循我试图剖析的图层格式。
考虑这段代码:
// Create an XML object out of the SVG
$svg = new SimpleXMLElement('floorplan.svg', null, true);
// Register the SVG namespace
$svg->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');
// Get the normal floorplan layer
$normal = $svg->xpath('svg:g[@id="Normal"]');
// If the normal layer has content, continue
if(count($normal) > 0) {
// If there are floors, continue
if(count($normal[0]->g > 0)) {
// Loop through each floor
foreach($normal[0]->g as $floor) {
// Declare the namespace for the floor
$floor->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');
// Select the base floorplan
$floorsvg = $floor->xpath('svg:g[@id="Base"]')[0];
var_dump($floorsvg);
echo $floorsvg->asXML(); // THIS CAUSES THE ERROR
}
}
}
当我在$floorsvg 上执行var_dump 时,它声明它是一个 SimpleXMLElement 对象:
object(SimpleXMLElement)[9]
public '@attributes' =>
array (size=1)
'id' => string 'Base' (length=4)
public 'g' =>
array (size=859)
0 => ...
但是,当我在对象上运行 asXML() 时,我会看到以下 PHP 错误:
PHP 致命错误:在非对象上调用成员函数 asXML()
我不确定为什么asXML() 会失败,因为它是一个对象。谁能解释为什么会出现这个问题以及我可能会尝试解决什么问题?
编辑:在上面添加echo $normal->asXML(); 会导致相同的错误。似乎xpath 导致对象以某种方式出现格式错误。
编辑 2: 可以在此处看到正在解析的 SVG 文件:http://pastebin.com/zK1yRFA7
【问题讨论】:
-
你能把你的 SVG 文件贴在某个地方吗?
-
@SeanBright 当然,我已经将我的帖子编辑到了 SVG 的 PasteBin 转储中。
-
所以我必须做出我在(现已删除)答案中建议的更改以避免语法错误,但是一旦我这样做了,您的代码就可以正常工作了。
-
我不确定它对你有什么作用。我在键盘上重新创建了这个问题:codepad.viper-7.com/sWMKIP