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.

Community
  • 1
  • 1
vishal_g
  • 3,871
  • 4
  • 21
  • 34
  • You typically wouldn't do this "in XML". You convert/parse the XML into a PHP array or object, do your merging, then output that back to XML. – deceze Dec 20 '11 at 09:31
  • @deceze K lets me try that thanks for your suggestion atleast i can start.. – vishal_g Dec 20 '11 at 09:41

1 Answers1

1

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);
Robin Michael Poothurai
  • 5,444
  • 7
  • 23
  • 36