You can use the SimpleXMLElement to work with XML in the PHP.
When using it, you will receive an element with the method xpath. With this method you can use expressions to filter some values, for example:
$livros = $simpleXML->xpath("//genero//*[contains(text(), 'genero')]/../..");
This way we access all the elements genero and we check with contains, if the text has the desired gender. Then we return to the node ISBN with the ../../
Complete code:
<?php
$pesquisa = "Assassinato";
$xml = <<<XML
<Livros>
<ISBN id="978-8547216696">
<genero>
<descricao>Direito</descricao>
<descricao>Psicologia</descricao>
</genero>
<titulo>
Insania Furens. Casos Verídicos de Loucura e Crime
</titulo>
<autor>
Guido Arturo Palomba
</autor>
<publicacao>
2017
</publicacao>
</ISBN>
<ISBN id="978-8594540386">
<genero>
<descricao>Psicologia</descricao>
</genero>
<titulo>
Arquivos Serial Killers. Made in Brazil e Louco ou Cruel
</titulo>
<autor>
Ilana Casoy
</autor>
<publicacao>
2017
</publicacao>
</ISBN>
<ISBN id="978-8594540133">
<genero>
<descricao>Histórias Reais</descricao>
<descricao>Assassinato e Mutilação</descricao>
</genero>
<titulo>
Casos de Família: Arquivos Richthofen e Arquivos Nardoni
</titulo>
<autor>
Ilana Casoy
</autor>
<publicacao>
2016
</publicacao>
</ISBN>
</Livros>
XML;
$simpleXML = new SimpleXMLElement($xml);
$livros = $simpleXML->xpath("//genero//*[contains(text(), '{$pesquisa}')]/../..");
foreach($livros as $livro) {
$titulo = trim(reset($livro->xpath("./titulo")[0]));
echo "Título: {$titulo}\n";
}
That one
XMLis generated through database records?– Valdeir Psr
I am doing in a local file for study, I load the xml of the same folder through file_get_contents.
– Eduardo Ribeiro