Change XML tags value

Asked

Viewed 950 times

3

I’m having a hard time. I’m unable to scan the XML file and change the values of the tags I need, they are NOTA and SERIE. I have to replace these tags with new values, someone knows how to do it ? 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>
  • just a hint, try using ". / "in the path, so it becomes much more practical for you instead of using the absolute path.

  • Yes, I won’t do it this way. The way will come dynamically from another class, I did so just to gain time and test rsrs. But anyway thank you.

  • You want to change the tags or their contents?

  • The contents of tags.

  • @Marloncastro if any of the answers solved your problem, please mark the solution.

2 answers

3


I found a solution. I did it this 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());
}
  • 1

    Sorry, but will you explain the code or just paste?

  • 1

    I will comment on it , it is because it is busy the day.

  • 1

    Your answer and Marcelo’s are practically the same thing, only changed that he used Simplexml and you used Domdocument, as the question is about the php-Document tag, so from my point of view your answer is correct, but for solving the problem both are equally correct :)

  • Yeah, as soon as he posted his, I had just managed to do rsrs. But I was reading , and they said that to change the content Domdocument is better , but anyway. Anyway it worked !

  • That’s not what I meant, you remarked, "Then we’ll look at it and see which one looks better," but technically they’re the same thing, just changed the API they used ;)

  • That’s right! Thank you..

  • 1

    Exactly @Guilherme Nascimento, I saw after I posted the tag php-domdocument, but as I did not feel the need and it was not explained why it was only this api, used the simpleXml.

  • 1

    @Marcelodeandrade I agree with you, if you will just read and edit simple things Simplexml is much easier, I use Domdocument when I want to do complex manipulations ;)

Show 3 more comments

1

If you just want to replace 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");
  • Thank you very much for your help, Marcelo. But right now I can solve too , I will post my answer also there we analyze and see which got better and we close the topic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.