0

I would like to create a new simplified xml based on an existing one: (using "simpleXml")

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

Because there are always colons in the element-tags, it will mess with "simpleXml", I tried to use the following solution->link.

How can I create a new xml with this structure:

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

the "instruction-element" gets its content from the former "xls:Instruction-element".

Here is the updated code: But unfortunately it never loops through:

$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();

there is no error-message, if I leave the "@"…

Community
  • 1
  • 1
algro
  • 639
  • 4
  • 8
  • 25

2 Answers2

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();
ajreal
  • 46,720
  • 11
  • 89
  • 119
  • Thanks a lot for the answer. unfortunately it never loops through. I updated the code above. The original xml can be found on gps.alaingroeneweg.com/route.xml – algro Dec 02 '10 at 22:41
1

You could use xpath to simplify things. Without knowing the full details, I don't know if it will work in all cases:

$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();

Output:

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

Edit: The file at http://www.gps.alaingroeneweg.com/route.xml is not the same as the XML you have in your question. You need to use a namespace like:

$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();

Output:

<?xml version="1.0"?>
<main><instruction>Start (Southeast) auf Sihlquai</instruction><instruction>Fahre rechts</instruction><instruction>Fahre halb links - Ziel erreicht!</instruction></main>
Matthew
  • 47,584
  • 11
  • 86
  • 98
  • Thanks for the answer, still no luck with that. I assume that it adresses the wrong initial-node. Hm because I'm completely new to php, even my debugging-skills are limited... With $instr you mean the original node (xls:)Instruction? www.gps.alaingroeneweg.com/route.xml – algro Dec 02 '10 at 22:40
  • The code produces that output (PHP 5.3) using the XML snippet you supplied. So the problem is either the XML you are using doesn't match or (unlikely) the version of PHP you have somehow produces different results. – Matthew Dec 02 '10 at 22:54
  • @algro, I've updated my answer with another version that uses that direct XML file. – Matthew Dec 02 '10 at 23:01
  • +1 decent answer than mine for the updated xml format (rather called is xls) – ajreal Dec 03 '10 at 06:05
  • great! it works now: no registerXPathNamespace needed, and I had to use the relative path (file_get_contents("route.xml")) Thanks a lot! – algro Dec 03 '10 at 10:36