I have the below code which creates an object and then converts the object into XML. As you can see I have this in a loop as I have multiple objects. I do not want to create a new XML file for each object, I want them all in the same XML file. I therefore somehow need to combine the objects prior to adding them to the XML within this loop. Anyone any ideas how?
$orderObj1 = new Order();
$orders = $orderObj1->getAllOrders();
foreach ($orders as $order => $orderdetail){
//create Order XML from object
$orderObj2 = new Order($orderdetail['id']);
}
$serializer = new XML_Serializer();
// set name for root element
$serializer->setOption("rootName", "orders");
//add the XML declaration
$serializer->setOption("addDecl", "true");
$result = $serializer->serialize($orderObj2);
if($result === true) {
$orders_xml = $serializer->getSerializedData();
}
echo $orders_xml;
They can't be a correct xml file unless you start messing with the serialiser output. Each one will start with an xml header (<?xml ...)
. There can only be one of them...
This isn't necessarily a problem, you could treat the output as a series of xml documents and chop them up (as a string) before attempting to deserialise (or any other form of xml process).
You could create an object that is a collection of xml documents, and then serialise that using CDATA tags.
In theory you could deep copy each node in the serialised object, sort out names spaces and then reverse the process for deserialisation, but that would be incredibly fragile....
I did the string manouevre recently it worked fine...
I seem to have sorted it. This was really simple. I simply needed to make and array of objects as below:
$ordersObj = array();
foreach ($orders as $order => $orderdetail){
//create Order XML from object
$ordersObj[] = new Order($db, $shopKeeperID, $orderdetail['id']);
}
I had no idea this would be possible, but it is working. Thanks for your help.