dtpk04526211 2015-04-07 17:19
浏览 44
已采纳

Php xml,用xml对象替换正在使用的节点

I'm facing an Xml problem. Probably something stupid but I can't see it...

Here is my Xml at start :

<combinations nodeType="combination" api="combinations">
    <combination xlink:href="http://localhost:8888/vmv/ps/api/combinations/1">
        <id><![CDATA[1]]></id>
    </combination>
    <combination xlink:href="http://localhost:8888/vmv/ps/api/combinations/2">
        <id><![CDATA[2]]></id>
    </combination>
</combinations>

So for each node, I make the API call then I want to replace the node by the returned value, like that :

$c_index = 0;
foreach($combinations->combination as $c){
    $new = apiCall($c->id); //load the new content
    $combinations->combination[$c_index] = $new;
    $c_index++;
}

If I dump the $new into the foreach, I got an simplexmlobject which is fine but if I dump the $combinations->combination[$x], I've got big string of blank...

I'd like to get :

<combinations nodeType="combination" api="combinations">
    <combination xlink:href="http://localhost:8888/vmv/ps/api/combinations/1">
        <my new tree>
            ....
        </my new tree>
    </combination>
    <combination xlink:href="http://localhost:8888/vmv/ps/api/combinations/2">
        <my new tree>
            ....
        </my new tree>
    </combination>
</combinations>

I must be missing something but what...? That's the question...

thanks for your help !

  • 写回答

1条回答 默认 最新

  • dongsaoshuo4326 2015-04-07 22:35
    关注

    You can change the current element $c of the foreach iteration by making use of a so called SimpleXMLElement-self-reference. By the magic nature of the SimpleXMLElement the entry for both array-access or property access of the number 0 (zero) represents the element itself. This can be used to change the elements value for example:

    foreach ($combinations->combination as $c) {
        $new  = apiCall($c->id); //load the new content
        $c[0] = $new;
    }
    

    The important part is $c[0] here. You could also write $c->{0} for property access with numbers. Example output (may api call returns "apiCall($paramter)" as string):

    <combinations nodeType="combination" api="combinations">
        <combination xlink:href="http://localhost:8888/vmv/ps/api/combinations/1">apiCall('1')</combination>
        <combination xlink:href="http://localhost:8888/vmv/ps/api/combinations/2">apiCall('2')</combination>
    </combinations>
    

    The example in full:

    $buffer = <<<XML
    <root xmlns:xlink="ns:1">
        <combinations nodeType="combination" api="combinations">
            <combination xlink:href="http://localhost:8888/vmv/ps/api/combinations/1">
                <id><![CDATA[1]]></id>
            </combination>
            <combination xlink:href="http://localhost:8888/vmv/ps/api/combinations/2">
                <id><![CDATA[2]]></id>
            </combination>
        </combinations>
    </root>
    XML;
    
    
    function apiCall($string)
    {
        return sprintf('apiCall(%s)', var_export((string)$string, true));
    }
    
    $xml = simplexml_load_string($buffer);
    
    $combinations = $xml->combinations;
    
    foreach ($combinations->combination as $c) {
        $new  = apiCall($c->id); //load the new content
        $c[0] = $new;
    }
    
    $combinations->asXML('php://output');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!