dongpochi9741 2019-07-30 14:19 采纳率: 100%
浏览 60
已采纳

在php中的另一个节点中附加xml节点

I want to insert different nodes inside the node. but i dont know how to do it dynamically

I have this xml file:

<?xml version='1.0'  encoding='UTF-8'?>
<article artId="4686453" artName="UHOPI20190218-012A" Author="" Comment="" PubDate="2019-02-18" Section="country">
 <head>
    <headline>this is the title </headline>
    <summary>
        autor name @twitter
    </summary>
  </head>
 <body>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer venenatis eleifend dui, at egestas sapien lobortis viverra.
<!-- insert pullquotes here -->
  </body>
<pullquote title="" catsList="" summary="" notes="" sectionColor="" sectionHead="">
    Lorem ipsum dolor sit amet
    <summary xml:space="preserve">Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    </summary>
  </pullquote>
<pullquote title="" catsList="" summary="" notes="" sectionColor="" sectionHead="">
    <headline xml:space="preserve">Some text</headline>
    <summary xml:space="preserve">Some text</summary>
    <summary xml:space="preserve">Some text</summary>
</pullquote>
</article>

I try this...but it doesnt work

$xml = simplexml_load_file("files/test2.xml");
$body = $xml->body;
$pull1 = $xml->pullquote[0];
$body->addChild($pull1);

I want to insert every node inside the node how can i do this?

  • 写回答

1条回答 默认 最新

  • doucigua0278 2019-08-08 19:09
    关注

    This is my solution:

    if($xmlData->body){
        $xmlIterator = new SimpleXMLIterator($xmlData->body->asXML());
        $xmlIterator->rewind();
        $txtBody = "";
    
        for($i=0; $i < ($xmlIterator->count()); $i++){
                $txtBody .= (trim($xmlIterator->current()) <> '') ? "<p>".trim($xmlIterator->current())."</p>" : "";
                                $xmlIterator->next();
                            }
                            #INSERT PULLQUOTE
                            foreach($xmlData->pullquote as $pull){
                                $txtBody .= $pull;
    
                                foreach ($pull->children() as $child){
                                    $txtBody .= $child;
                                }
    
                            }
    
                            $data = $txtBody;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分