Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6983807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:28:04+00:00 2026-05-27T18:28:04+00:00

I need to work with XML documents and I need to copy some fields

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:28:05+00:00Added an answer on May 27, 2026 at 6:28 pm

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that has many xml-like elements such as this one: <document
I have some sort of ... xml document as below : <file src=136090000-136100000> <member
I need help making AOP work. What am I missing here? <?xml version=1.0 encoding=UTF-8?>
I need to work with some old C++ code that was developed in Visual
I need to work with an old repository, whose db/format contains 2 From this
I have an input XML document something like this: <text> <p> Download the software
I have the following XML: <HTML> <HEAD> <META name=GENERATOR content=Microsoft HTML Help Workshop 4.1
I have two xml documents with about 1000 elements in each of them and
I need to transform one XML document into another using XSLT (for now from
I need to process XML documents of varying formats into records in a MySQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.