Change tag value of an XML

3

I'm having a hard time. I am not able to scan the XML file and change the values of the tags that I need, they are NOTA and SERIE . I have to replace these tags with new values, does anyone know how to do this? I researched, but the structure of my XML is making me more difficult. Follow the codes.

public function corrigirXML() {
    $dom = new DOMDocument();
    $dom->load("C:\BSI\INFOCLOUD\ENTRADA\".$this->getCaminhoXmlCorrecao());

    $root = $dom->documentElement;
    $index = $root->getElementsByTagName('IndexValue');
    print_r($index);
    for ($i =0; $i<count($index); $i++) {
        $type = $index->getElementsByTagName('Label')->item($i)->textContent;
        $title = $index->getElementsByTagName('Value')->item($i)->textContent;
        echo $type." - ".$title."<br>";
    }
}

XML structure

 <?xml version="1.0"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Number>61</Number>
<PageCount>1</PageCount>
<IndexValues>
 <IndexValue>
  <Label>TIPO_DOCUMENTO</Label>
  <Value>CANHOTOS</Value>
</IndexValue>
<IndexValue>
  <Label>NOTA</Label>
  <Value>001954884</Value>
</IndexValue>
 <IndexValue>
  <Label>SERIE</Label>
  <Value>1</Value>
 </IndexValue>
</IndexValues>
</Document>
    
asked by anonymous 08.03.2017 / 19:40

2 answers

3

I found a solution. I did it that way!

  public function corrigirXML() {
    //Setando o DOM e versão XML
    $DOMDocument = new DOMDocument('1.0', 'UTF-8');
    $DOMDocument->preserveWhiteSpace = false;
    //Informando o caminho do XML
    $DOMDocument->load("C:\BSI\INFOCLOUD\ENTRADA\" . $this->getCaminhoXmlCorrecao());
    //Pegando a tag para para poder fazer o foreach
    $products = $DOMDocument->getElementsByTagName('IndexValue');
    //Achando as tags filhas
    foreach ($products as $product) {
        // Guardando a tag label para a verificação
        $label = $product->getElementsByTagName('Label')->item(0)->nodeValue;
        // Se a label for iguala a nota ele muda a tag value para o valor que preciso para poder mudar o a tag no XML
        if ($label == "NOTA") {
            $product->getElementsByTagName("Value")->item(0)->nodeValue = $this->getNotaCorrecao();
        } elseif ($label == "SERIE") {
            $product->getElementsByTagName("Value")->item(0)->nodeValue = $this->getSerieCorrecao();
        }
    }
    //Salvando alterações
    echo $DOMDocument->save("C:\BSI\INFOCLOUD\ENTRADA\" . $this->getCaminhoXmlCorrecao());
}
    
08.03.2017 / 20:55
1

If you just want to override the values, go through the nodes and make the change as follows:

<?php

$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Number>61</Number>
   <PageCount>1</PageCount>
   <IndexValues>
      <IndexValue>
         <Label>TIPO_DOCUMENTO</Label>
         <Value>CANHOTOS</Value>
      </IndexValue>
      <IndexValue>
         <Label>NOTA</Label>
         <Value>001954884</Value>
      </IndexValue>
      <IndexValue>
         <Label>SERIE</Label>
         <Value>1</Value>
      </IndexValue>
   </IndexValues>
</Document>
XML;

$parsedXml = simplexml_load_string($xml);
foreach($parsedXml->IndexValues->IndexValue as $item)
{
    if($item->Label == 'NOTA'){
        $item->Value = 'xxxxxx';
    }

    if($item->Label == 'SERIE'){
        $item->Value = 'ABC123';
    }
}

$parsedXml->asXML("newdoc.xml");
    
08.03.2017 / 20:48