为了账号安全,请及时绑定邮箱和手机立即绑定

将 xml 节点附加到 php 中的另一个节点内

将 xml 节点附加到 php 中的另一个节点内

PHP
SMILET 2021-11-05 13:06:10
我想在节点内插入不同的节点。但我不知道如何动态地做到这一点我有这个 xml 文件:<?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>我试试这个……但它不起作用$xml = simplexml_load_file("files/test2.xml");$body = $xml->body;$pull1 = $xml->pullquote[0];$body->addChild($pull1);我想在节点内插入每个节点我该怎么做?
查看完整描述

1 回答

?
UYOU

TA贡献1878条经验 获得超4个赞

这是我的解决方案:


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;

}


查看完整回答
反对 回复 2021-11-05
  • 1 回答
  • 0 关注
  • 111 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信