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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,