douhuxi4145 2017-11-10 01:59
浏览 48
已采纳

我必须从另一个xml中创建一个新的xml。 我还是不知道怎么办呢

I have to make a new xml using the value from another xml. This is the xml code. I tried using xpath, but then I became confuse in order to display those value. Thanks for your help

<rss>
    <item id='1234'>
        <g:name>Gray</g:name>
        <g:description>It's a Bag</g:description>
        <g:detailed_images>
            <g:detailed_image>abcd</g:detailed_image>
            <g:detailed_image>abcde</g:detailed_image>
            <g:detailed_image>abcdef</g:detailed_image>
        </g:detailed_images>
    </item>
<rss>

The new xml format should be:

<rss>
<item>
<name></name>
<description></description>
<detailed_images>
  <detailed_image></detailed_image>
  <detailed_image></detailed_image>
</detailed_images>
</item>
</rss>

My bad, this is the php code that is still in progress. I haven't really figure it all yet. I'm still new to xml too. :D

<?php 
    $document = new DOMDocument;
    $document->preserveWhiteSpace = false;           
    $document->load('xml_edit_feeds.xml');
    $xpath = new DOMXPath($document);

    $xml = new DOMDocument;
    $rss = $xml->appendChild($xml->createElement('rss'));   

    foreach ($xpath->evaluate('//item') as $item) {
        //create tag item
        $createItem = $rss->appendChild($xml->createElement('item'));

        //getting item's attribute value
        $valueID = $item->getAttribute('id');

        //create attribute
        $itemAttribute = $xml->createAttribute('id');
        $itemAttribute->value = $valueID;
        $createItem->appendChild($itemAttribute);

        $result[] = [
            'g:name' => $xpath->evaluate('string(g:name)', $item),
            'g:description' => $xpath->evaluate('string(g:description)', $item),
            'g:detailed_images' => $xpath->evaluate('string(g:detailed_images)', $item)
          ];
    }

 ?>
  • 写回答

1条回答 默认 最新

  • dongruo4601 2017-11-10 08:31
    关注

    It would be a lot clearer using SimpleXML, but as you've started with DOM, I will stick with it and base the answer on that.

    $document = new DOMDocument;
    $document->preserveWhiteSpace = false;
    $document->load('xml_edit_feeds.xml');
    $xpath = new DOMXPath($document);
    
    $xml = new DOMDocument;
    $rss = $xml->appendChild($xml->createElement('rss'));
    
    foreach ($xpath->query('//item') as $item) {
        //create tag item
        $createItem = $rss->appendChild($xml->createElement('item'));
    
        //getting item's attribute value
        $valueID = $item->getAttribute('id');
    
        //create attribute
        $itemAttribute = $xml->createAttribute('id');
        $itemAttribute->value = $valueID;
        $createItem->appendChild($itemAttribute);
    
        $description = $item->getElementsByTagName("description");
        $descriptionE = $xml->createElement("description", $description[0]->nodeValue );
        $createItem->appendChild($descriptionE);
    
        $dImages = $item->getElementsByTagName("detailed_image");
        $dImagesE = $xml->createElement("detailed_images");
        $createItem->appendChild($dImagesE);
        foreach ( $dImages as $image )  {
            $img = $xml->createElement("detailed_image", $image->nodeValue );
            $dImagesE->appendChild($img);
    
        }
    }
    echo $xml->saveXML();
    

    It is a case of extracting each piece of data from the original document (here picking the elements out by getElementsByTagName) and then creating the equivalent node in the new document.

    With the images, this has to use a loop to add each image in one at a time.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?