0

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

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by ivan3510
0

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");

Edited by ivan3510: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.