What is the best approach to merge(join) XML data?:

File#1:

<users>
	<user>
	     <name>Mark</name>
	     <id>100</id>
	</user>
	<user>
	     <name>Fred</name>
	     <id>100</id>
	</user>

</users>

File#2:

<users>
	<user>
	     <id>100</id>
	     <lat>50</lat>
	     <lon>122</lon>
        </user>
</users>

Desired output (only keep records where a match is found):

<users>
	<user>
	   <id>100</id>
	   <lat>50</lat>
	   <lon>122</lon>
           <name>Mark</name>
        </user>
</users>

Thanks in advance!

M

When you get data:

$name="";
$id="";
$lat="";
$lon="";
$xml=new SimpleXMLElement("path/to/file.xml",null,true);
foreach($xml as $data){
if($name=="" && $data->name) $name=$data->name;
if($id=="" && $data->id) $id=$data->id;
if($lat=="" && $data->lat) $lat=$data->lat;
if($lon=="" && $data->lon) $lon=$data->lon;
}

So, now you have these variables. Now you just put them in new xml file:

$dom=new DomDocument();
$dom->load("path/to/file.xml");
..... //create and append
$dom->save("path/to/file.xml");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.