Skip to content

Commit

Permalink
Added XML namespace support (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
vpominchuk authored Nov 7, 2022
1 parent 8eb5533 commit 688b469
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions src/SitemapParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,27 @@ protected function isSitemapURL($url)
);
}

/**
* Convert object to array recursively
*
* @param $object
* @return array
*/
protected function objectToArray($object)
{
if (is_object($object) || is_array($object)) {
$ret = (array)$object;

foreach($ret as &$item) {
$item = $this->objectToArray($item);
}

return $ret;
} else {
return $object;
}
}

/**
* Parse Json object
*
Expand All @@ -382,9 +403,36 @@ protected function parseJson($type, \SimpleXMLElement $json)
if (!isset($json->$type)) {
return false;
}
foreach ($json->$type as $url) {
$this->addArray($type, (array)$url);

$nameSpaces = $json->getDocNamespaces();

if (!empty($nameSpaces)) {
$tags = ["namespaces" => []];

foreach ($json->$type as $node) {
foreach ($nameSpaces as $nameSpace => $value) {
if ($nameSpace != "") {
$tags["namespaces"] = array_merge(
$tags["namespaces"],
[
$nameSpace => $this->objectToArray(
$node->children($nameSpace, true)
)
]
);
} else {
$tags = array_merge($tags, (array)$node);
}
}

$this->addArray($type, $tags);
}
} else {
foreach ($json->$type as $node) {
$this->addArray($type, (array)$node);
}
}

return true;
}

Expand Down

0 comments on commit 688b469

Please sign in to comment.