xml parsing with php -


i create new simplified xml based on 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 colons in element-tags, mess "simplexml", tried use following solution->link.

how can create new xml structure:

<main>   <instruction>start</instruction>   <instruction>end</instruction> </main> 

the "instruction-element" gets content former "xls:instruction-element".

here updated code: unfortunately 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() $child){    print_r("xml_has_childs");    $new_xml->addchild('instruction', $child->routeinstruction->instruction); } echo $new_xml->asxml(); 

there no error-message, if leave "@"…

you use xpath simplify things. without knowing full details, don't know if work in cases:

$source = "route.xml"; $xmlstr = file_get_contents($source); $xml = @simplexml_load_string($xmlstr); $new_xml = simplexml_load_string('<main/>'); foreach ($xml->xpath('//instruction') $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: file @ http://www.gps.alaingroeneweg.com/route.xml not same xml have in question. need use namespace like:

$xml = @simplexml_load_string(file_get_contents('http://www.gps.alaingroeneweg.com/route.xml')); $xml->registerxpathnamespace('xls', 'http://www.opengis.net/xls'); // not needed  $new_xml = simplexml_load_string('<main/>'); foreach ($xml->xpath('//xls:instruction') $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> 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -