2009-06-16 10 views
6

Chcę napisać funkcję, która analizuje (teoretycznie) nieznaną strukturę danych XML w równoważnej tablicy PHP.iteracja nad nieznaną strukturą XML z PHP (DOM)

Oto mój przykładowy XML:

<?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> 

zmodyfikowałem ten DOM iteracji funkcji z 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/>"; 
    } 

Teraz, ze względu na sposób olewam elementów do tablicy I” m nadpisywanie dowolnych elementów, które współdzielą nazwę węzła (ponieważ najlepiej chcę nadać kluczom te same nazwy, co ich węzły początkowe). Moja rekursja nie jest wspaniała ... Jednak nawet jeśli opróżnię nawiasy klamrowe - druga warstwa węzłów nadal będzie w postaci wartości na pierwszej warstwie (zobacz tekst węzła opisu).

Ktoś ma jakieś pomysły, jak mogę to lepiej skonstruować?

Odpowiedz

2

może być lepiej po prostu zadzierać jakiś kod off netto

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

Możesz być zainteresowany SimpleXML lub xml_parse_into_struct.

$ arraydata jest ani przekazywany do kolejnych zaproszeń do xml2array() nie jest wartością zwrot używany, tak tak „Moja rekurencji nie jest wielki ...” jest prawdą ;-)
Aby dołączyć nowego elementu do istniejąca tablica może zawierać puste nawiasy kwadratowe, $ arr [] = 123; $ arr [$ x] [] = 123;

+0

I uczynił metodę, która przechodzi tablicę z powrotem, ale chcę, aby dowiedzieć się, dlaczego to nie widząc te węzły 2nd tier. Jak jeszcze dodać ostatni zestaw pustych nawiasów na tablicy o nieznanej głębokości? – sunwukung

+0

Mam na myśli, czy widziałeś scenariusz w poście nad twoim ... to jest bestia! – sunwukung