du0173 2017-09-29 11:02
浏览 31
已采纳

使用PHP识别XML元素并将父节点复制到外部XML文件

I am parsing an XML file (source.xml) in PHP and need to identify instances where the <property> node contains the <rent> element.

Once identified the entire <property> parent node for that entry should be copied to a separate XML file (destination.xml).

On completion of the copy that <property> node should be removed from the source.xml file.

Here is an example of the source.xml file:

<?xml version="1.0" encoding="utf-8"?>
<root>
    <property>
        ...
        <rent>
            <term>long</term>
            <freq>month</freq>
            <price_peak>1234</price_peak>
            <price_high>1234</price_high>
            <price_medium>1234</price_medium>
            <price_low>1234</price_low>
        </rent>
        ...
    </property>
</root>

I've tried using DOM with the below code however I'm not getting any results at all despite their being hundreds of nodes that match the above requisites. Here is what I have so far:

$destination = new DOMDocument;
$destination->preserveWhiteSpace = true;
$destination->load('destination.xml');

$source = new DOMDocument;
$source->load('source.xml');

$xp = new DOMXPath($source);

foreach ($xp->query('/root/property/rent[term/freq/price_peak/price_high/price_medium/price_low]') as $item) {

    $newItem = $destination->documentElement->appendChild(

        $destination->createElement('property')

    );

    foreach (array('term', 'freq', 'price_peak', 'price_high', 'price_medium', 'price_low') as $elementName) {

        $newItem->appendChild(
            $destination->importNode(
                $item->getElementsByTagName($elementName)->property(0),
                true
            )
        );
    }
}

$destination->formatOutput = true;
echo $destination->saveXml();

I've only started learning about DOMDocument and it's uses so I'm obviously messing up somewhere so any help is appreciated. Many thanks.

  • 写回答

3条回答 默认 最新

  • douyong4842 2017-09-29 15:00
    关注

    The difficulty is when your trying to copy a node from one document to another. You can try and re-create the node, copying all of the components across, but this is hard work (and prone to errors). Instead you can import the node from one document to another using importNode. The second parameter says copy all child elements as well.

    Then deleting the element from the original document is a case of getting the item to 'delete itself from it's parent' which sounds odd, but thats how this code works.

    <?php
    error_reporting ( E_ALL );
    ini_set ( 'display_errors', 1 );
    
    $destination = new DOMDocument;
    $destination->preserveWhiteSpace = true;
    $destination->loadXML('<?xml version="1.0" encoding="utf-8"?><root></root>');
    
    $source = new DOMDocument;
    $source->load('NewFile.xml');
    
    $xp = new DOMXPath($source);
    $destRoot = $destination->getElementsByTagName("root")->item(0);
    foreach ($xp->query('/root/property[rent]') as $item) {
        $newItem = $destination->importNode($item, true);
        $destRoot->appendChild($newItem);
        $item->parentNode->removeChild($item);
    }
    
    echo "Source:".$source->saveXML();
    $destination->formatOutput = true;
    echo "destination:".$destination->saveXml();
    

    With the destination, I prime it with the basic <root> element and then add in the contents from there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据