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

138
Visualizações
Getting the value of XML field matching a property via XPATH

I've got a really odd XML schema that's causing me unecessary grief and woe.

I need to get the value of an IMAGEFILENAME node that has a property of "hide".

The XML schema looks something like:

<PHOTOS>
    <IMAGETHUMBFILENAME/>
    <IMAGECAPTION>
        This is a caption
    </IMAGECAPTION>
    <PRINTQUALITYIMAGE>
        /mylocation/filename1.jpg
    </PRINTQUALITYIMAGE>
    <IMAGEFILENAME pictype="show">
        /mylocation/filename2.jpg
    </IMAGEFILENAME>
    <IMAGETHUMBFILENAME/>
    <IMAGECAPTION>This is another caption</IMAGECAPTION>
    <PRINTQUALITYIMAGE>
        /mylocation/filename3.jpg
    </PRINTQUALITYIMAGE>
    <IMAGEFILENAME pictype="hide">
        /mylocation/filename4.jpg
    </IMAGEFILENAME>
    <IMAGETHUMBFILENAME/>
</PHOTOS>

And I've managed to come up with the following XPATH using PHP:

$nodes = $xml->xpath('/PHOTOS/IMAGEFILENAME[@pictype="hide"]');
var_dump($nodes);

When I do a dump of the $nodes var what I'd hope to see (and what I want) is to get the value /mylocation/filename4.jpg. Instead what I'm getting is:

array(1) {
  [0]=>
  object(SimpleXMLElement)#333 (1) {
    ["@attributes"]=>
    array(1) {
      ["pictype"]=>
      string(10) "hide"
    }
  }
}

I've tried various combinations of /parent, /text() and /node() but with no joy at all.

Please somebody tell me what a muppet I'm being and put me out of my misery. Either that or is the schema being problematic?

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

0

So, you have array of SimpleXMLElements.

To get string representation of SimpleXMLElement you can just echo it:

$nodes = $xml->xpath('/PHOTOS/IMAGEFILENAME[@pictype="hide"]');
echo $nodes[0];   // I used `[]` notation to get first element of array

To use string representation of SimpleXMLElement later in your code you can convert it to string explicitly:

$nodes = $xml->xpath('/PHOTOS/IMAGEFILENAME[@pictype="hide"]');
$node_str = strval($nodes[0]);  // still `[]` notation
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