2010-12-02 7 views
0

Je voudrais créer un nouveau xml simplifié basé sur un existant: (en utilisant « SimpleXML »)parsing XML avec php

<?xml version="1.0" encoding="UTF-8"?> 
<xls:XLS> 
    <xls:RouteInstructionsList> 
    <xls:RouteInstruction> 
     <xls:Instruction>Start</xls:Instruction> 
    </xls:RouteInstruction> 
    </xls:RouteInstructionsList> 
    <xls:RouteInstructionsList> 
    <xls:RouteInstruction> 
     <xls:Instruction>End</xls:Instruction> 
    </xls:RouteInstruction> 
    </xls:RouteInstructionsList> 
</xls:XLS> 

Parce qu'il ya toujours deux points dans les balises d'éléments, il gâchera avec "simpleXml", j'ai essayé d'utiliser la solution suivante->link.

Comment puis-je créer un nouveau xml avec cette structure:

<main> 
    <instruction>Start</instruction> 
    <instruction>End</instruction> 
</main> 

la "instruction-élément" tire son contenu de l'ancien "xls: Instruction-élément".

Voici le code mis à jour: Mais malheureusement il boucle jamais par:

$source = "route.xml"; 
$xmlstr = file_get_contents($source); 
$xml = @simplexml_load_string($xmlstr); 
$new_xml = simplexml_load_string('<main/>'); 
foreach($xml->children() as $child){ 
    print_r("xml_has_childs"); 
    $new_xml->addChild('instruction', $child->RouteInstruction->Instruction); 
} 
echo $new_xml->asXML(); 

il n'y a pas message d'erreur, si je laisse le "@" ...

Répondre

1

Vous pouvez utiliser xpath pour simplifier les choses. Sans connaître les détails, je ne sais pas si cela va fonctionner dans tous les cas:

$source = "route.xml"; 
$xmlstr = file_get_contents($source); 
$xml = @simplexml_load_string($xmlstr); 
$new_xml = simplexml_load_string('<main/>'); 
foreach ($xml->xpath('//Instruction') as $instr) { 
    $new_xml->addChild('instruction', (string) $instr); 
} 
echo $new_xml->asXML(); 

Sortie:

<?xml version="1.0"?> 
<main><instruction>Start</instruction><instruction>End</instruction></main> 

Edit: Le fichier à http://www.gps.alaingroeneweg.com/route.xml n'est pas le même que le XML que vous avez dans votre question. Vous devez utiliser un espace de noms comme:

$xml = @simplexml_load_string(file_get_contents('http://www.gps.alaingroeneweg.com/route.xml')); 
$xml->registerXPathNamespace('xls', 'http://www.opengis.net/xls'); // probably not needed 
$new_xml = simplexml_load_string('<main/>'); 
foreach ($xml->xpath('//xls:Instruction') as $instr) { 
    $new_xml->addChild('instruction', (string) $instr); 
} 
echo $new_xml->asXML(); 

Sortie:

<?xml version="1.0"?> 
<main><instruction>Start (Southeast) auf Sihlquai</instruction><instruction>Fahre rechts</instruction><instruction>Fahre halb links - Ziel erreicht!</instruction></main> 
+0

Merci pour la réponse, toujours pas de chance avec ça. Je suppose qu'il adresse le mauvais noeud initial. Hm parce que je suis complètement nouveau pour PHP, même mes compétences de débogage sont limitées ... Avec $ instr vous voulez dire le nœud d'origine (xls:) Instruction? www.gps.alaingroeneweg.com/route.xml – algro

+0

Le code produit cette sortie (PHP 5.3) en utilisant l'extrait XML que vous avez fourni. Donc, le problème est que le XML que vous utilisez ne correspond pas ou (improbable) la version de PHP que vous avez en quelque sorte produit des résultats différents. – Matthew

+0

@algro, j'ai mis à jour ma réponse avec une autre version qui utilise ce fichier XML direct. – Matthew

3
/* the use of @ is to suppress warning */ 
$xml = @simplexml_load_string($YOUR_RSS_XML); 
$new_xml = simplexml_load_string('<main/>'); 
foreach ($xml->children() as $child) 
{ 
    $new_xml->addChild('instruction', $child->RouteInstruction->Instruction); 
} 

/* to print */ 
echo $new_xml->asXML(); 
+0

Merci beaucoup pour la réponse. Malheureusement, il ne passe jamais à travers. J'ai mis à jour le code ci-dessus. Le fichier XML original peut être trouvé sur gps.alaingroeneweg.com/route.xml – algro

+0

+1 pour la réponse initiale – algro