Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

204
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda