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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于下拉菜单选项关联的问题
  • ¥15 如何修改pca中的feature函数
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R