2011-12-22 5 views
0

XML文書を操作する必要があり、いくつかのフィールドを別のXMLファイルにコピーする必要があります。 私はこのフィールドを持っている:XML属性をコピーするPHP

<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> 

私は が、私はこのコードを使用しますが、それは動作しません...別のXMLではなく、属性データを持つすべての要素をコピーする必要があります。

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

「それはdoesnの「仕事」は、あなたが与えることができる最も役に立たない情報です。何がうまくいかない?それがうまくいかないことをどのように知っていますか?エラーメッセージが出ますか? – hoppa

+0

すみません。つまり、構造上見つけた人物ごとにをコピーするだけです。私も属性をコピーする必要があります。 あなたは今私を助けることができますか? – Max

+0

私の側からの間違いを見るためにはちょっとコメントしてください。私たちはお互いを助けるためにここにいると考えるべきです。 – Max

答えて

3

、あなたがDOMDocumを使用しなければならないのにテストされていませんSimpleXMLではなくentです

DOMDocumentを使用してある文書から別の文書にコピーするコードです。注:これは2段階のプロセスです。まずノードを$ ndTempとして文書にインポートします。次に、インポートした$ ndTempを指定した親に追加します(ルートdocumentElementだけを使用しますが、別のノードにすることもできます)。

注:あなただけの単純なコピーをやっている場合、あなたはXSLを使用して検討する必要がありますが、それは別のポストだ...

入力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(); 

?> 

出力

<?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> 
+0

このような良い応答ありがとう! – Max

0
私はこれが仕事だと思う

、あなたは(つまり、別のドキュメントからノードを追加する)は、XML文書を操作したい場合は

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

ありがとうございます..それは動作していないようです。 これは私がこれを作る前に: '$ cast = $ root-> appendChild( $ xmldoc-> createElement(" cast "));' 私はその情報を使って私に手を差し伸べることができるかどうかわかりません! – Max

+0

@Max:createElementを使用する必要がある場合、属性を手作業でコピーする必要があります。 – RageZ

+0

を参照してください。ルート要素を作成します。ここでは、すべてを追加します。すべてにを追加し、のコレクションを追加する必要があります。だから私はその要素 "キャスト"を作成する理由は何ですか? "親"をコピーするために何をすることができますすべては、属性を含む$映画を形成??? – Max