簡體   English   中英

從父級到子級的 XML 更改值

[英]XML change value from parent to child

我有這個 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();

一種選擇是將xpathaddChild與位置值一起使用:

$url = file_get_contents("archive.xml");
$xml = simplexml_load_string($url);
$changes = $xml->xpath("/destinos/destino");

foreach ($changes as $change) {
    $text = (string)$change->location;
    foreach ($change->xpath("programas/item") as $i) {
        $i->addChild("location", $text);
    }
}
header('Content-Type: application/xml');

echo $xml->asXML();

輸出

<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 演示

使用 SimpleXML,您可以只使用對象符號來訪問文檔的各種元素,這不再需要 XPath 並且還可以使代碼更具可讀性......

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

$xml = simplexml_load_string($url);

foreach ($xml->destino as $destino) {
    // Process each item
    foreach ( $destino->programas->item as $item )  {
        // Set the location from the destino location value
        $item->location = (string)$destino->location;
    }
}

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

echo $xml->asXML();

需要注意的一件事是,在使用 SimpleXML 時,根節點(在本例中為<destinos> )是$xml對象。 這就是$xml->destino訪問<destino>元素的原因。

使用 DOM,您可以將位置節點的克隆附加到相應的 item 元素。

$document = new DOMDocument();
$document->load($url);
$xpath = new DOMXpath($document);

// iterate the location child of the destino elements
foreach($xpath->evaluate('//destino/location') as $location) {
    // iterate the item nodes inside the same parent node 
    foreach ($xpath->evaluate('parent::*/programas/item', $location) as $item) {
        // append a copy of the location to the item
        $item->appendChild($location->cloneNode(TRUE));
    }

}

echo $document->saveXML();

非常感謝你的幫助。 它運作完美。

令人懷疑的是,如果我只想從“ item”節點中獲取XML,應該執行哪種循環?

我試過了:


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

echo $item->asXML();;

我只有一個結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM