【问题标题】:xml parsing with php用php解析xml
【发布时间】: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();

没有错误信息,如果我离开“@”...

【问题讨论】:

    标签: php xml parsing simplexml


    【解决方案1】:
    /* 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();
    

    【讨论】:

    • 非常感谢您的回答。不幸的是,它永远不会循环通过。我更新了上面的代码。原始 xml 可以在 gps.alaingroeneneweg.com/route.xml 上找到
    【解决方案2】:

    您可以使用 xpath 来简化事情。在不了解全部细节的情况下,我不知道它是否适用于所有情况:

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

    输出:

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

    编辑:http://www.gps.alaingroeneweg.com/route.xml 的文件与您在问题中的 XML 不同。您需要使用如下命名空间:

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

    输出:

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

    【讨论】:

    • 感谢您的回答,仍然没有运气。我假设它处理了错误的初始节点。嗯,因为我对php完全陌生,即使我的调试技能也很有限...... $instr你的意思是原始节点(xls:)指令? www.gps.alaingroeneneweg.com/route.xml
    • 代码使用您提供的 XML sn-p 生成该输出 (PHP 5.3)。所以问题要么是您使用的 XML 不匹配,要么(不太可能)您拥有的 PHP 版本以某种方式产生不同的结果。
    • @algro,我已经用另一个使用该直接 XML 文件的版本更新了我的答案。
    • 对于更新的 xml 格式(更称为 xls),比我的 +1 体面的答案
    • 太棒了!它现在可以工作了:不需要 registerXPathNamespace,我不得不使用相对路径 (file_get_contents("route.xml")) 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 2010-11-23
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多