Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    Parsare un XML - consigli

    Ciao a tutti,

    dovrei parsare il contenuto di un file xml in testo piano .

    Avrei pensato ad Xpath. Vorrei un sistema che mi permetta di parsare tutto il contenuto senza conoscere la struttura XML.

    Cosa potrei fare?

  2. #2
    xke proprio XPath?

    javascript nun te piaze? php? quali scopi devi raggiungere?
    Questa volta, più che un voto.. è favoreggiamento.

  3. #3
    Ciao e grazie per la risposta,

    mi sono informato ed ho abbandonato Xpath. Sto usando PHP, ora.

    Ti spiego cosa devo fare...

    1 - Prendere in input un file XML
    2 - Modificare il testo contenuto negli elementi
    3 - Salvare l'XML mantenendo la stessa struttura gerachica

    una traduzione di un xml, praticamente...

    allora ho pensato di iniziare parsando il file xml in un array php.

    ho trovato questo codice:
    Codice PHP:
    function xml2array($xml) {
            
    $xmlary = array();
                   
            
    $reels '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s';
            
    $reattrs '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';

            
    preg_match_all($reels$xml$elements);

            foreach (
    $elements[1] as $ie => $xx) {
                    
    $xmlary[$ie]["name"] = $elements[1][$ie];
                   
                    if (
    $attributes trim($elements[2][$ie])) {
                            
    preg_match_all($reattrs$attributes$att);
                            foreach (
    $att[1] as $ia => $xx)
                                    
    $xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];
                    }

                    
    $cdend strpos($elements[3][$ie], "<");
                    if (
    $cdend 0) {
                            
    $xmlary[$ie]["text"] = substr($elements[3][$ie], 0$cdend 1);
                    }

                    if (
    preg_match($reels$elements[3][$ie]))
                            
    $xmlary[$ie]["elements"] = xml2array($elements[3][$ie]);
                    else if (
    $elements[3][$ie]) {
                            
    $xmlary[$ie]["text"] = $elements[3][$ie];
                    }
            }

            return 
    $xmlary;

    non ho capito come si usa...

  4. #4
    ho trovato quest'altro codice, che sembra funzionare meglio:
    http://mysrc.blogspot.com/2007/02/ph...backwards.html

    Codice PHP:


    // XML to Array
    function xml2ary(&$string) {
        
    $parser xml_parser_create();
        
    xml_parser_set_option($parserXML_OPTION_CASE_FOLDING0);
        
    xml_parse_into_struct($parser$string$vals$index);
        
    xml_parser_free($parser);

        
    $mnary=array();
        
    $ary=&$mnary;
        foreach (
    $vals as $r) {
            
    $t=$r['tag'];
            if (
    $r['type']=='open') {
                if (isset(
    $ary[$t])) {
                    if (isset(
    $ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
                    
    $cv=&$ary[$t][count($ary[$t])-1];
                } else 
    $cv=&$ary[$t];
                if (isset(
    $r['attributes'])) {foreach ($r['attributes'] as $k=>$v$cv['_a'][$k]=$v;}
                
    $cv['_c']=array();
                
    $cv['_c']['_p']=&$ary;
                
    $ary=&$cv['_c'];

            } elseif (
    $r['type']=='complete') {
                if (isset(
    $ary[$t])) { // same as open
                    
    if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
                    
    $cv=&$ary[$t][count($ary[$t])-1];
                } else 
    $cv=&$ary[$t];
                if (isset(
    $r['attributes'])) {foreach ($r['attributes'] as $k=>$v$cv['_a'][$k]=$v;}
                
    $cv['_v']=(isset($r['value']) ? $r['value'] : '');

            } elseif (
    $r['type']=='close') {
                
    $ary=&$ary['_p'];
            }
        }    
        
        
    _del_p($mnary);
        return 
    $mnary;
    }

    // _Internal: Remove recursion in result array
    function _del_p(&$ary) {
        foreach (
    $ary as $k=>$v) {
            if (
    $k==='_p') unset($ary[$k]);
            elseif (
    is_array($ary[$k])) _del_p($ary[$k]);
        }
    }

    // Array to XML
    function ary2xml($cary$d=0$forcetag='') {
        
    $res=array();
        foreach (
    $cary as $tag=>$r) {
            if (isset(
    $r[0])) {
                
    $res[]=ary2xml($r$d$tag);
            } else {
                if (
    $forcetag$tag=$forcetag;
                
    $sp=str_repeat("\t"$d);
                
    $res[]="$sp<$tag";
                if (isset(
    $r['_a'])) {foreach ($r['_a'] as $at=>$av$res[]=$at=\"$av\"";}
                
    $res[]=">".((isset($r['_c'])) ? "\n" '');
                if (isset(
    $r['_c'])) $res[]=ary2xml($r['_c'], $d+1);
                elseif (isset(
    $r['_v'])) $res[]=$r['_v'];
                
    $res[]=(isset($r['_c']) ? $sp '')."</$tag>\n";
            }
            
        }
        return 
    implode(''$res);
    }

    // Insert element into array
    function ins2ary(&$ary$element$pos) {
        
    $ar1=array_slice($ary0$pos); $ar1[]=$element;
        
    $ary=array_merge($ar1array_slice($ary$pos));
    }

    /*
        Working with XML. Usage: 
        $xml=xml2ary(file_get_contents('1.xml'));
        $link=&$xml['ddd']['_c'];
        $link['twomore']=$link['onemore'];
        // ins2ary(); // dot not insert a link, and arrays with links inside!
        echo ary2xml($xml);
    */ 
    Formatta bene l'xml e permette di passare da xml ad array php e viceversa...

    potreste spiegarmi come ciclare l'array con la funzione xml2ary($xml);?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.