猿问

XML 将值从父级更改为子级

我有这个 XML:


<destinos>

 <destino>

   <location>Spain</location>

     <programas>

       <item></item>

       <item></item>

     </programas>

 </destino>

 <destino>

   <location>France</location>

     <programas>

       <item></item>

       <item></item>

     </programas>

 </destino>

</destinos>


我需要在每个“项目”中包含或复制“位置”的值,但我无法这样做。


<destinos>

 <destino>

   <location>Spain</location>

     <programas>

       <item>

        <location>Spain</location>

       </item>

       <item>

        <location>Spain</location>

       </item>

     </programas>

 </destino>

 <destino>

   <location>France</location>

     <programas>

       <item>

        <location>France</location>

       </item>

       <item>

        <location>France</location>

       </item>

     </programas>

 </destino>

</destinos>


我对PHP一无所知,我一直在阅读,但找不到解决方案。


如果有人可以帮助我并解释我将非常感激。


我的代码:


$url = file_get_contents("archive.xml");


$xml = simplexml_load_string($url); 


$changes = $xml->xpath("//*[starts-with(local-name(), 'item')]");


foreach ($changes as $change) 

    $change[0] = $xml->destinos->destino->location;


header('Content-Type: application/xml');


echo $xml->asXML();


慕斯709654
浏览 152回答 3
3回答

江户川乱折腾

使用 DOM,您可以将位置节点的克隆附加到相应的项目元素。$document = new DOMDocument();$document->load($url);$xpath = new DOMXpath($document);// iterate the location child of the destino elementsforeach($xpath->evaluate('//destino/location') as $location) {&nbsp; &nbsp; // iterate the item nodes inside the same parent node&nbsp;&nbsp; &nbsp; foreach ($xpath->evaluate('parent::*/programas/item', $location) as $item) {&nbsp; &nbsp; &nbsp; &nbsp; // append a copy of the location to the item&nbsp; &nbsp; &nbsp; &nbsp; $item->appendChild($location->cloneNode(TRUE));&nbsp; &nbsp; }}echo $document->saveXML();

莫回无

使用 SimpleXML,您可以只使用对象表示法来访问文档的各种元素,这样就不再需要 XPath 并且还可以使代码更具可读性...$url = file_get_contents("archive.xml");$xml = simplexml_load_string($url);foreach ($xml->destino as $destino) {&nbsp; &nbsp; // Process each item&nbsp; &nbsp; foreach ( $destino->programas->item as $item )&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Set the location from the destino location value&nbsp; &nbsp; &nbsp; &nbsp; $item->location = (string)$destino->location;&nbsp; &nbsp; }}header('Content-Type: application/xml');echo $xml->asXML();需要注意的一点是,在使用 SimpleXML 时,根节点(<destinos>在本例中)是$xml对象。这就是$xml->destino访问<destino>元素的原因。

萧十郎

一种选择是将xpath与addChild与位置值一起使用:$url = file_get_contents("archive.xml");$xml = simplexml_load_string($url);$changes = $xml->xpath("/destinos/destino");foreach ($changes as $change) {&nbsp; &nbsp; $text = (string)$change->location;&nbsp; &nbsp; foreach ($change->xpath("programas/item") as $i) {&nbsp; &nbsp; &nbsp; &nbsp; $i->addChild("location", $text);&nbsp; &nbsp; }}header('Content-Type: application/xml');echo $xml->asXML();输出<destinos>&nbsp; &nbsp; <destino>&nbsp; &nbsp; &nbsp; &nbsp; <location>Spain</location>&nbsp; &nbsp; &nbsp; &nbsp; <programas>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <item><location>Spain</location></item>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <item><location>Spain</location></item>&nbsp; &nbsp; &nbsp; &nbsp; </programas>&nbsp; &nbsp; </destino>&nbsp; &nbsp; <destino>&nbsp; &nbsp; &nbsp; &nbsp; <location>France</location>&nbsp; &nbsp; &nbsp; &nbsp; <programas>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <item><location>France</location></item>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <item><location>France</location></item>&nbsp; &nbsp; &nbsp; &nbsp; </programas>&nbsp; &nbsp; </destino></destinos>
随时随地看视频慕课网APP
我要回答