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 "@"...

asked Dec 2, 2010 at 16:48

2 Answers 2

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();
answered Dec 2, 2010 at 17:25

1 Comment

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
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>
answered Dec 2, 2010 at 22:16

5 Comments

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
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.
@algro, I've updated my answer with another version that uses that direct XML file.
+1 decent answer than mine for the updated xml format (rather called is xls)
great! it works now: no registerXPathNamespace needed, and I had to use the relative path (file_get_contents("route.xml")) Thanks a lot!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.