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 7510547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:15:15+00:00 2026-05-29T23:15:15+00:00

First of all, I need to Find a parent node by a specific child

  • 0

First of all, I need to Find a parent node by a specific child node value in an xml doc; Then copy some specific children nodes from the parent node to another xml doc.

For instance:

DESTINATION FILE: ('destination.xml') 
<item>
    <offerStartDate>2012-15-02</offerStartDate>
    <offerEndDate>2012-19-02</offerEndDate>
    <title>Item Title</title> 
    <rrp>14.99</rrp>
    <offerPrice>9.99</offerPrice>
</item> 

and

SOURCE FILE: ('source.xml') 
<items> 
    <item> 
         <title>Item A</title> 
         <description>This is the description for Item A</description> 
         <id>1003</id>
         <price>
             <rrp>10.00</rrp>
             <offerPrice>4.99</offerPrice>
         </price>
         <offer>
             <deal>
                 <isLive>0</isLive>
             </deal>
         </offer>
    </item>
    <item> 
         <title>Item B</title> 
         <description>This is the description for Item B</description> 
         <id>1003</id>
         <price>
             <rrp>14.99</rrp>
             <offerPrice>9.99</offerPrice>
         </price>
         <offer>
             <deal>
                 <isLive>1</isLive>
             </deal>
         </offer>
    </item> 
    <item> 
         <title>Item C</title> 
         <description>This is the description for Item C</description> 
         <id>1003</id>
         <price>
             <rrp>9.99</rrp>
             <offerPrice>5.99</offerPrice>
         </price>
         <offer>
             <deal>
                 <isLive>0</isLive>
             </deal>
         </offer>
    </item> 

I want to find the parent node <item> that has it’s child node <isLive> value set to “1”. Then copy other children nodes of the parent node to the destination xml.

e.g. If parent node <item> has its child node <isLive> set to 1. Copy <title>, <rrp> and <offerPrice> nodes and their values and add them to the destination file as children nodes as shown above.

Pardon my technical lingo if I have not used them correctly.

Many thanks for the help guys!

  • 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-29T23:15:17+00:00Added an answer on May 29, 2026 at 11:15 pm

    With SimpleXml (demo):

    $dItems = simplexml_load_file('destination.xml');
    $sItems = simplexml_load_file('source.xml');
    foreach ($sItems->xpath('/items/item[offer/deal/isLive=1]') as $item) {
        $newItem = $dItems->addChild('item');
        $newItem->addChild('title', $item->title);
        $newItem->addChild('rrp', $item->price->rrp);
        $newItem->addChild('offerprice', $item->price->offerPrice);
    }
    echo $dItems->saveXML();
    

    With DOM (demo):

    $destination = new DOMDocument;
    $destination->preserveWhiteSpace = false;
    $destination->load('destination.xml');
    $source = new DOMDocument;
    $source->load('source.xml');
    $xp = new DOMXPath($source);
    foreach ($xp->query('/items/item[offer/deal/isLive=1]') as $item)
    {
        $newItem = $destination->documentElement->appendChild(
            $destination->createElement('item')
        );
        foreach (array('title', 'rrp', 'offerPrice') as $elementName) {
            $newItem->appendChild(
                $destination->importNode(
                    $item->getElementsByTagName($elementName)->item(0),
                    true
                )
            );
        }
    }
    $destination->formatOutput = true;
    echo $destination->saveXml();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement program by c# application. first of all I need web
First of all, I don't need a textual comparison so Beyond Compare doesn't do
All I need this for is strcpy(). I want to see whether the first
First of all I'm not sure this is even possible, however I need to
i need to redirect all of the stdout of a program except the first
I've got a fairly complex XML document, where I need to remove specific nodes
I am beginner to programming for android. First of all i need that my
First of all, some links to pages I've used for reference: A SO question
I need to find all matches in a string for a given regex. I've
first of all, I'd like to explain why i need this. i need to

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.