dqoys62082 2012-02-20 09:48
浏览 42

组合多个对象

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;
  • 写回答

2条回答 默认 最新

  • doufei8691 2012-02-20 10:10
    关注

    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...

    评论

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染