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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化