2009-06-16 16 views
6

Desidero scrivere una funzione che analizzi una struttura di dati XML (teoricamente) sconosciuta in un array PHP equivalente.iterazione su struttura XML sconosciuta con PHP (DOM)

Ecco il mio XML di esempio:

<?xml version="1.0" encoding="UTF-8"?> 
<content> 

<title>Sample Text</title> 

<introduction> 
    <paragraph>This is some rudimentary text</paragraph> 
</introduction> 
<description> 
    <paragraph>Here is some more text</paragraph> 
    <paragraph>Even MORE text</paragraph> 
    <sub_section> 
     <sub_para>This is a smaller, sub paragraph</sub_para> 
     <sub_para>This is another smaller, sub paragraph</sub_para> 
    </sub_section> 
</description> 
</content> 

ho modificato questa funzione l'iterazione DOM da devarticles:

$data = 'path/to/xmldoc.xml'; 
$xmlDoc = new DOMDocument(); #create a DOM element 
$xmlDoc->load($data); #load data into the element 
$xmlRoot = $xmlDoc->firstChild; #establish root 

function xml2array($node) 
    { 
    if ($node->hasChildNodes()) 
    { 
$subNodes = $node->childNodes; 
    foreach ($subNodes as $subNode) 
     { 
     #filter node types 
     if (($subNode->nodeType != 3) || (($subNode->nodeType == 3))) 
      { 
      $arraydata[$subNode->nodeName]=$subNode->nodeValue; 
      } 
     xml2array($subNode); 
     } 
     } 
     return $arraydata; 
    } 
//The getNodesInfo function call 

$xmlarray = xml2array($xmlRoot); 


// print the output - with a little bit of formatting for ease of use... 
foreach($xmlarray as $xkey) 
    { 
    echo"$xkey<br/><br/>"; 
    } 

Ora, a causa del modo in cui sto passando gli elementi alla matrice I' m sovrascrivendo gli elementi che condividono il nome di un nodo (dal momento che preferisco dare alle chiavi lo stesso nome dei loro nodi di origine). La mia ricorsione non è eccezionale ... Tuttavia, anche se svuoto le parentesi, il secondo livello di nodi è ancora presente come valori sul primo livello (vedi il testo del nodo della descrizione).

Qualcuno ha qualche idea su come posso costruirlo meglio?

risposta

2

si potrebbe essere meglio solo sbavatura po 'di codice fuori dalla rete

http://www.bin-co.com/php/scripts/xml2array/

/** 
    * xml2array() will convert the given XML text to an array in the XML structure. 
    * Link: http://www.bin-co.com/php/scripts/xml2array/ 
    * Arguments : $contents - The XML text 
    *    $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value. 
    *    $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance. 
    * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure. 
    * Examples: $array = xml2array(file_get_contents('feed.xml')); 
    *    $array = xml2array(file_get_contents('feed.xml', 1, 'attribute')); 
    */ 
    function xml2array($contents, $get_attributes=1, $priority = 'tag') { 
1

Potreste essere interessati a SimpleXML o xml_parse_into_struct.

$ arraydata non è né passato alle chiamate successive alla xml2array(), né è il valore di ritorno utilizzato, quindi sì "Il mio ricorsione non è grande ..." è vero ;-)
Per aggiungere un nuovo elemento a un array esistente puoi usare parentesi quadre vuote, $ arr [] = 123; $ arr [$ x] [] = 123;

+0

ho fatto fare un metodo che passa la matrice indietro, ma voglio capire perché non è vedere quei 2 ° nodi di primo livello. Inoltre, come si continua ad aggiungere quell'ultima serie di parentesi vuote su una matrice di profondità sconosciuta? – sunwukung

+0

Voglio dire, hai visto la sceneggiatura nel post sopra il tuo .... è una bestia! – sunwukung

Problemi correlati