Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
Get children of an XML node with unknown structure

I'm trying to modify an XML document which contains some node that I can identify by name. For example, I might want to modify the <abc>some text</abc> node in a document (which I can identify by the tag name abc)

The problem I'm facing currently is that I don't know the exact structure of this document. I don't know what the root element is called and I don't know which children might contain this <abc> node.

I tried using SimpleXML<...> but this does not allow me to read arbitrary element children:

$xml = new SimpleXMLElement($xmlString);

foreach ($xml->children() as $child) {
    // code here doesnt execute
}

I'm considering building my own XML parser which would have this simple functionality, but I cannot believe that simply iterating over all child nodes of a node (eventually recursively) is not something that is supported by PHP. Hopefully someone can tell me what I'm missing. Thanks!

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Use DOMDocument

$dom = new DOMDocument();
@$dom->loadXML($xmlString);     

foreach($dom->getElementsByTagName('item') as $item) { 

    if ($item->hasChildNodes()) {

        foreach($item->childNodes as $i) {

            YOUR CODE HERE
        }

    }
}
over 4 years ago · Santiago Trujillo Relatório

0

I found the solution moments after posting, after being stuck on it for a while..

SimpleXML<...> does not have these features, but the DOMDocument and associated classes do;

$dom = new DOMDocument();
$dom->loadXml($xmlString);

foreach($dom->childNodes as $child) {
    if ($child->nodeName == "abc") {
        $child->textContent = "modified text content";
    }
}

Documentation for future reference, here: http://php.net/manual/en/book.dom.php

Thanks for your help.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda