我需要解析这个具有一些自定义标记的XML文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<glz:Config xmlns:glz="http://www.glizy.org/dtd/1.0/">
<glz:Import src="config.xml" />
<glz:Group name="thumbnail">
<glz:Param name="width" value="200" />
<glz:Param name="height" value="*" />
</glz:Group>
</glz:Config>当它到达标记<glz:Import src="config.xml" />时,它需要解析文件config.xml,该文件包含以下内容:
<?xml version="1.0" encoding="utf-8"?>
<glz:Config xmlns:glz="http://www.glizy.org/dtd/1.0/">
<glz:Group name="folder">
<glz:Param name="width" value="100" />
<glz:Param name="height" value="200" />
</glz:Group>
</glz:Config>最终结果应该是如下所示的数组。它包含两个已解析文件的值:
$result['thumbnail/width'] = 200;
$result['thumbnail/height'] = '*';
$result['folder/width'] = 100;
$result['folder/height'] = 200;这就是我管理XML解析的方式。我的问题是,我不知道如何将新的结果与已经(旧)解析的结果合并。你可以在这里看到我的代码:
function parseFile(){
$reader = new XMLReader;
$reader->open($this->fileName);
while ($reader->read()){
if ($reader->name == 'glz:Group')
{
$groupName = $reader->getAttribute('name');
$reader->read();
$reader->read();
while ($reader->name == 'glz:Param')
{
if (strpos($reader->getAttribute('name'),'[]') == true)
{
$arrayGroupName = substr($reader->getAttribute('name'), 0, -2);
if(empty($filters[$groupName.'/'.$arrayGroupName]))
{
$filters[$groupName.'/'.$arrayGroupName] = array();
array_push($filters[$groupName.'/'.$arrayGroupName],$this->castValue($reader->getAttribute('value')));
$this->result[$groupName."/".$arrayGroupName] = $filters[$groupName.'/'.$arrayGroupName];
}
else
{
array_push($filters[$groupName.'/'.$arrayGroupName],$this->castValue($reader->getAttribute('value')));
$this->result[$groupName."/".$arrayGroupName] = $filters[$groupName.'/'.$arrayGroupName];
}
}
else
{
$this->result[$groupName."/".$reader->getAttribute('name')] = $this->castValue($reader->getAttribute('value'));
}
$reader->read();
$reader->read();
}
}
else if ($reader->name == 'glz:Param')
{
if (strpos($reader->getAttribute('name'),'[]') == true)
{
$arrayGroupName = substr($reader->getAttribute('name'), 0, -2);
if(empty($filters[$arrayGroupName]))
{
$filters[$arrayGroupName] = array();
array_push($filters[$arrayGroupName],$this->castValue($reader->getAttribute('value')));
$this->result[$$arrayGroupName] = $filters[$arrayGroupName];
}
else
{
array_push($filters[$arrayGroupName],$this->castValue($reader->getAttribute('value')));
$this->result[$arrayGroupName] = $filters[$arrayGroupName];
}
}
else
{
$this->result[$reader->getAttribute('name')] = $this->castValue($reader->getAttribute('value'));
}
}
else if ($reader->name == 'glz:Import')
{
$file = $reader->getAttribute('src');
$newConfig = new Config($file);
$newConfig->parseFile();
}
}
return $this->result;
}如何在每次找到标记时合并解析文件得到的结果?
非常感谢!
https://stackoverflow.com/questions/50820655
复制相似问题