PHP: Utilizzo SimpleXML a ciclo attraverso tutti i livelli di un file XML
Domanda
Ho una funzione che SimpleXML utilizza per restituire il primo livello di nodi in un file XML e scrivere in una lista non ordinata:
function printAssetMap() {
$xml = simplexml_load_file(X_ASSETS);
$assets = $xml->asset;
$html = '<ul>'."\n";
foreach ($assets as $asset) {
$html .= '<li id="asset'.$asset->asset_assetid.'"><ins> </ins><a href="#">'.$asset->asset_name.' ['.$asset->asset_assetid.']</a></li>'."\n";
}//end foreach
$html .= '</ul>'."\n";
return $html;
}// printAssetMap()
XML che sto usando, che ha nidificato i nodi:
<?xml version="1.0"?>
<assets>
<asset>
<asset_name>Home</asset_name>
<asset_url>/home</asset_url>
<asset_assetid>1</asset_assetid>
</asset>
<asset>
<asset_name>Projects</asset_name>
<asset_url>/projects</asset_url>
<asset_assetid>2</asset_assetid>
<asset>
<asset_name>Portfolio</asset_name>
<asset_url>/projects/portfolio</asset_url>
<asset_assetid>3</asset_assetid>
</asset>
<asset>
<asset_name>Current Jobs</asset_name>
<asset_url>/projects/current-jobs</asset_url>
<asset_assetid>4</asset_assetid>
</asset>
</asset>
</assets>
Ora, sto iniziando aggiungere nodi figlio dei nodi che attualmente sto tornando. C'è un modo per scorrere tutti i livelli di nodi figlio in un file XML, anche se non so quanti livelli ci sono, e aggiungi quelli alla mia lista?
Soluzione
Quindi, in pratica quello che dovete fare è una funzione che prende ogni bambino <asset/>
del nodo corrente, costruisce il codice HTML poi controlla se il nodo corrente ha <asset/>
figli propri e mantiene recursing più in basso l'albero.
Ecco come si può fare:
function printAssetMap()
{
return printAssets(simplexml_load_file(X_ASSETS));
}
function printAssets(SimpleXMLElement $parent)
{
$html = "<ul>\n";
foreach ($parent->asset as $asset)
{
$html .= printAsset($asset);
}
$html .= "</ul>\n";
return $html;
}
function printAsset(SimpleXMLElement $asset)
{
$html = '<li id="asset'.$asset->asset_assetid.'"><ins> </ins><a href="#">'.$asset->asset_name.' ['.$asset->asset_assetid.']</a>';
if (isset($asset->asset))
{
// has <asset/> children
$html .= printAssets($asset);
}
$html .= "</li>\n";
return $html;
}
A proposito, mi sarei aspettato una funzione denominata "printX" per davvero Stampa o eco qualcosa, invece di restituirlo. Forse si dovrebbe nominare quelle funzioni "buildX"?
Altri suggerimenti
è necessario utilizzare una funzione ricorsiva. ecco un esempio che emette una matrice da XML. E 'dalla documentazione PHP - http://www.php.net/manual /en/ref.simplexml.php . È possibile modificare il presente per produrre una lista.
<?php
function XMLToArray($xml)
{
if ($xml instanceof SimpleXMLElement) {
$children = $xml->children();
$return = null;
}
foreach ($children as $element => $value) {
if ($value instanceof SimpleXMLElement) {
$values = (array)$value->children();
if (count($values) > 0) {
$return[$element] = XMLToArray($value);
} else {
if (!isset($return[$element])) {
$return[$element] = (string)$value;
} else {
if (!is_array($return[$element])) {
$return[$element] = array($return[$element], (string)$value);
} else {
$return[$element][] = (string)$value;
}
}
}
}
}
if (is_array($return)) {
return $return;
} else {
return $false;
}
}
?>
<?php
function all_nodes($xml){
$all=array();
function add($node){
array_push($all,$node);
foreach($node as $child){
add($child);
}
}
add($xml->documentElement);
return $all;
}
?>
Ciò restituirà un array con tutti i nodi del documento.