How to search an xml file with php?

Asked

Viewed 581 times

0

<ISBN id = "1">
        <genero>
            <descricao>
                Suspense
            </descricao>
            <descricao>
                Ficcao Policial
            </descricao>
        </genero>
        <titulo>
            Assassinato no expresso do oriente
        </titulo>
        <autor>
            Agatha Christie
        </autor>
        <publicacao>
            1934
        </publicacao>
    </ISBN>

Guys, I want to create a PHP system so that based on the xml file above, the user can type, for example, suspense and do a search for all books with this genre. Does anyone know how I can do that? (The original xml is composed of 8 books structured in this way)

  • That one XML is generated through database records?

  • I am doing in a local file for study, I load the xml of the same folder through file_get_contents.

1 answer

0


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";
}
  • You know how I print several tags with the same title, for example , in my xml there are several description tags there following your code would print only the index "0".

  • @Eduardoribeiro did not understand.

Browser other questions tagged

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