XML响应与PHP

7eumitmz  于 2023-04-10  发布在  PHP
关注(0)|答案(2)|浏览(122)

我试图弄清楚如何使用PHP和MySQL发送包含此XML结构的响应。
我使用echo "<?xml version...";创建响应,但我尝试使用SimpleXML对象。

查询对象:

$data_workers -〉$data_workers["id"]; $data_workers["name"];
$data_works -〉$data_works["worker_id"]; $data_works["id"]; $data_works["date"];

验证码:

<?xml version="1.0"?>
<response status="1" status_description="">
    <worker id="1" name="John Smith" phone="1956456456" >
        <works>
            <work id="1" date="01-01-2023" total="350" />
            <work id="2" date="02-01-2023" total="350" />
        </works>
        <tools>
            <tool type="Hammer" color="red"/>
            <tool type="Tape" color="blue"/>
        </tools>
        <pendings/>
    </worker>
</response>

有人能告诉我,这是否可以用PHP处理类似于JSON响应?

30byixjq

30byixjq1#

您可以尝试以下代码

$data = ['name' => 'test','email' => 'test@gm.com'];// change this with your array
 $xml = new SimpleXMLElement('<root/>');
 array_walk_recursive($data, array($xml, 'addChild'));
 echo $xml->asXML();
dpiehjr4

dpiehjr42#

从对象/数据结构到XML的通用转换是复杂的。您需要有关嵌套以及属性或文本内容的其他信息。以下是基于JSON的格式,包括XML的所有信息,如JSONML(JSON标记语言)。其他概念使用带有注解的模型的类/接口定义。
另一种方法是使用DOM或XMLWriter生成特定的XML输出。SimpleXML是一种抽象,只适用于某些(基本)格式。
在DOM中,你使用文档上的方法来创建不同的节点类型(元素,文本节点,属性,注解,...),然后使用父节点的方法来附加/插入节点。如果你从头开始创建文档,你只需要appendChild()(或PHP 8中新的append())。创建的节点上的方法用于修改它(添加属性,子节点,...)。
以下是您提供的数据示例:

$data_workers = [
     ['id'=>"1", 'name'=>"John Smith", 'phone'=>"1956456456"],
     ['id'=>"2", 'name'=>"Jane Doe", 'phone'=>"1956456457"]
];

$data_works = [
     ['id'=>"1", 'worker_id'=>"1", 'date'=>"01-01-2023", 'total'=>"350"],
     ['id'=>"2", 'worker_id'=>"1", 'date'=>"01-01-2023", 'total'=>"360"],
     ['id'=>"3", 'worker_id'=>"2", 'date'=>"01-01-2023", 'total'=>"370"]
];

$document = new DOMDocument();
// create and append the document element "response"
$document->appendChild(
    $response = $document->createElement('response')
);
// add attributes to it
$response->setAttribute('status', "1");
$response->setAttribute('status_description', '');

// iterate the worker data
foreach ($data_workers as $workerData) {
    // create "worker" element and append to "response"
    $response->appendChild(
        $worker = $document->createElement('worker')
    );
    $worker->setAttribute('id', $workerData['id']);
    $worker->setAttribute('name', $workerData['name']);
    $worker->setAttribute('phone', $workerData['phone']);
    
    // filter works by worker_id 
    $worksOfWorker = array_filter(
        $data_works, 
        fn($current) => $current['worker_id'] === $workerData['id']
    );
     
    $worker->appendChild(
        $works = $document->createElement('works')
    );
    
    foreach ($worksOfWorker as $workData) {
        $works->appendChild(
            $work = $document->createElement('work')
        );
        $work->setAttribute('id', $workData['id']);
        $work->setAttribute('date', $workData['date']);
        $work->setAttribute('total', $workData['total']);
    }
    
}

$document->formatOutput = true;
echo $document->saveXML();

输出:

<?xml version="1.0"?>
<response status="1" status_description="">
  <worker id="1" name="John Smith" phone="1956456456">
    <works>
      <work id="1" date="01-01-2023" total="350"/>
      <work id="2" date="01-01-2023" total="360"/>
    </works>
  </worker>
  <worker id="2" name="Jane Doe" phone="1956456457">
    <works>
      <work id="3" date="01-01-2023" total="370"/>
    </works>
  </worker>
</response>

相关问题