Copy XML attributes PHP

妖精的绣舞 提交于 2020-01-17 08:16:58

问题


I need to work with XML documents and I need to copy some fields to another XML file. I have this field:

<cast>
    <person name="Nanda Anand" character="" job="Director" id="589088" thumb="" department="Directing" url="http://www.themoviedb.org/person/589088" order="0" cast_id="1000"/>
    <person name="Lynn Collins" character="" job="Actor" id="21044" thumb="http://cf2.imgobject.com/t/p/w185/berS11tKvXqTFThUWAYrH279cvn.jpg" department="Actors" url="http://www.themoviedb.org/person/21044" order="0" cast_id="1001"/>
</cast>

I need to copy all the elements to the another XML but with the attributes data.. I use this code, but it doesn't work:

foreach ($movies->movies->movie->cast->children() as $person)
            {
            $person = $cast->appendChild(
                    $xmldoc->createElement(("person"), str_replace("&", "&amp;",$person)));
            }

回答1:


If you want to manipulate an XML document (ie adding nodes from one document to another), you should be using DOMDocument, not SimpleXML.

Here's some code to copy from one document to another using DOMDocument. Note, it's a two step process. First import the node into the document as $ndTemp. Second, append the imported $ndTemp to a specified parent (I just use the root documentElement, but it could be another node).

NOTE: if you're just doing a simple copy, you may want to consider using XSL, but that's another post...

Input XML (movie.xml)

<xml>
<movie name='first'>
    <cast>
        <person name="Nanda Anand" character="" job="Director" id="589088" thumb="" department="Directing" url="http://www.themoviedb.org/person/589088" order="0" cast_id="1000"/>
        <person name="Lynn Collins" character="" job="Actor" id="21044" thumb="http://cf2.imgobject.com/t/p/w185/berS11tKvXqTFThUWAYrH279cvn.jpg" department="Actors" url="http://www.themoviedb.org/person/21044" order="0" cast_id="1001"/>
    </cast>
</movie>
<movie name='Second'>
    <cast>
        <person name="Zaphod Beeblebrox" character="" job="Director" id="589088" thumb="" department="Directing" url="http://www.themoviedb.org/person/589088" order="0" cast_id="1000"/>
    </cast>
</movie>
</xml>

PHP

<?php
    $xml = new DOMDocument();
    $strFileName = "movie.xml";
    $xml->load($strFileName);

    $xmlCopy = new DOMDocument();
    $xmlCopy->loadXML( "<xml/>" );

    $xpath = new domxpath( $xml );
    $strXPath = "/xml/movie/cast/person";

    $elements = $xpath->query( $strXPath, $xml );
    foreach( $elements as $element ) {
        $ndTemp = $xmlCopy->importNode( $element, true );
        $xmlCopy->documentElement->appendChild( $ndTemp );
    }
    echo $xmlCopy->saveXML();

?>

Output

<?xml version="1.0"?>
<xml>
    <person name="Nanda Anand" character="" job="Director" id="589088" thumb="" department="Directing" url="http://www.themoviedb.org/person/589088" order="0" cast_id="1000" />
    <person name="Lynn Collins" character="" job="Actor" id="21044" thumb="http://cf2.imgobject.com/t/p/w185/berS11tKvXqTFThUWAYrH279cvn.jpg" department="Actors"
    url="http://www.themoviedb.org/person/21044" order="0" cast_id="1001" />
    <person name="Zaphod Beeblebrox" character="" job="Director" id="589088" thumb="" department="Directing" url="http://www.themoviedb.org/person/589088" order="0" cast_id="1000" />
</xml>



回答2:


I think this would work, not tested though

foreach ($movies->movies->movie->cast->children() as $person)
{
   $cast->appendChild($person);
}


来源:https://stackoverflow.com/questions/8602503/copy-xml-attributes-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!