【发布时间】:2010-12-02 16:48:08
【问题描述】:
我想在现有的基础上创建一个新的简化 xml: (使用“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>
由于element-tags中总是有冒号,会和“simpleXml”混淆,我尝试使用以下解决方案->link。
如何创建具有这种结构的新 xml:
<main>
<instruction>Start</instruction>
<instruction>End</instruction>
</main>
“instruction-element”从之前的“xls:Instruction-element”获取其内容。
这是更新后的代码: 但不幸的是它永远不会循环:
$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();
没有错误信息,如果我离开“@”...
【问题讨论】: