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

The Archive Base Latest Questions

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

Possible Duplicate: Best XML Parser for PHP —–First XML—–str1 <HOME> <USER_DETAIL> <LOCATION><![CDATA[MUMBAI]]></LOCATION> <NAME><![CDATA[RAVI]]></NAME> <ID><![CDATA[101]]></ID>

  • 0

Possible Duplicate:
Best XML Parser for PHP

—–First XML—–str1

<HOME>
    <USER_DETAIL>
        <LOCATION><![CDATA[MUMBAI]]></LOCATION>
        <NAME><![CDATA[RAVI]]></NAME>
        <ID><![CDATA[101]]></ID>
    </USER_DETAIL>
    <USER_DETAIL>
        <LOCATION><![CDATA[PUNE]]></LOCATION>
        <NAME><![CDATA[MANISH]]></NAME>
        <ID><![CDATA[102]]></ID>
    </USER_DETAIL>
</HOME>

—Second XML—-str2

<USER_DATA>
    <DATA>
        <DOB><![CDATA[2/11/1959]]></DOB>
        <STATUS><![CDATA[single]]></STATUS>
        <PROFILE_PIC><![CDATA[101.JPG]]></PROFILE_PIC>
        <ID><![CDATA[101]]></ID>
    </DATA>
    <DATA>
        <DOB><![CDATA[6/8/1987]]></DOB>
        <STATUS><![CDATA[married]]></STATUS>
        <PROFILE_PIC><![CDATA[102.JPG]]></PROFILE_PIC>
        <ID><![CDATA[102]]></ID>
    </DATA>
</USER_DATA>

I am new to XML.
I have two xml as above I want common xml with all the data of user. In two XML common thing is ID of user. so is there any way to pass xml into function and compare the id in second xml and get the detais of that perticular user and forms a required(Common with all detais) xml.
str1 and str2 is the php variable which contains the above xml respectively.

  • 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:09:41+00:00Added an answer on May 27, 2026 at 6:09 pm

    use below codes to generate a new XML, you can change the nodes based on your requirements. in case you have both XMLs in a file you can use simplexml_load_file($filename); to load the XML and assign it to $str1 and $str2

    $str1 = "<HOME>
    <USER_DETAIL>
        <LOCATION><![CDATA[MUMBAI]]></LOCATION>
        <NAME><![CDATA[RAVI]]></NAME>
        <ID><![CDATA[101]]></ID>
    </USER_DETAIL>
    <USER_DETAIL>
        <LOCATION><![CDATA[PUNE]]></LOCATION>
        <NAME><![CDATA[MANISH]]></NAME>
        <ID><![CDATA[102]]></ID>
    </USER_DETAIL>
    </HOME>";
    
    $str2 = "<USER_DATA>
    <DATA>
        <DOB><![CDATA[2/11/1959]]></DOB>
        <STATUS><![CDATA[single]]></STATUS>
        <PROFILE_PIC><![CDATA[101.JPG]]></PROFILE_PIC>
        <ID><![CDATA[101]]></ID>
    </DATA>
    <DATA>
        <DOB><![CDATA[6/8/1987]]></DOB>
        <STATUS><![CDATA[married]]></STATUS>
        <PROFILE_PIC><![CDATA[102.JPG]]></PROFILE_PIC>
        <ID><![CDATA[102]]></ID>
    </DATA>
    </USER_DATA>";
    
    $xml1 = simplexml_load_string($str1);
    $xml2 = simplexml_load_string($str1);
    //print_r($xml);
    
    $tempArr = array();
    foreach( $xml1 as $obj)  {
    $id = $obj->ID;
    $tempArr["$id"]['LOCATION'] = (string)$obj->LOCATION;
    $tempArr["$id"]['NAME'] = (string)$obj->NAME;
    }
    foreach( $xml2 as $obj)  {
    $id = $obj->ID; 
    $tempArr["$id"]['DOB'] = (string)$obj->DOB;
    $tempArr["$id"]['STATUS'] = (string)$obj->STATUS;
    $tempArr["$id"]['PROFILE_PIC'] = (string)$obj->PROFILE_PIC;
    }
    
    //print_r($tempArr);
    
    $xml = new DOMDocument('1.0', 'iso-8859-1');
    
    $doc = $xml->createElement('DOCUMENT');
    $doc = $xml->appendChild($doc);
    
    foreach( $tempArr as $ky=>$val )  {
    
    $rnod = $xml->createElement('USER_DETAIL');
    $rnod = $doc->appendChild($rnod);
    //$rnod = $xml->appendChild($rnod);
    
    $dataN0 = $xml->createElement('ID');
    $dataN0 = $rnod->appendChild($dataN0);
    
    $nodV = $xml->createTextNode($ky);
    $dataN0->appendChild($nodV);
    
    foreach($val as $k=>$v)  {
        $dataN1 = $xml->createElement($k);
        $dataN1 = $rnod->appendChild($dataN1);
    
        $nodV1 = $xml->createTextNode($v);
        $dataN1->appendChild($nodV1);
    }
    }
    
    $newXml = $xml->saveXML();
    echo htmlspecialchars($newXml);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Best XML Parser for PHP I have an XML code like as
Possible Duplicate: Best XML Parser for PHP I'm submitting a form which returns an
Possible Duplicate: Best XML Parser for PHP I have a string with XML data
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a
Possible Duplicate: Best way to stop SQL Injection in PHP I have seen some
Possible Duplicate: Best practice: Import mySQL file in PHP; split queries How to import
Possible Duplicate: What is the best method to merge two PHP objects? I have
Possible Duplicate: Best method to parse various custom XML documents in Java HI all,
Possible Duplicate: What is the best way to build XML in C# code? what's
Possible Duplicate: Best methods to parse HTML with PHP I wish to get the

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.