Reputation: 199
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("&", "&",$person)));
}
Upvotes: 0
Views: 844
Reputation: 2923
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>
Upvotes: 3
Reputation: 27313
I think this would work, not tested though
foreach ($movies->movies->movie->cast->children() as $person)
{
$cast->appendChild($person);
}
Upvotes: 0