Ir para conteúdo
Fórum Script Brasil
  • 0

Fazer busca com XML no php


Uriel Abadon Cassiel Lopes

Pergunta

 <?php
if(isset($_GET['categoria']) && !empty($_GET['categoria'])){}
 ?>
 <form action="" method="get"><input name="categoria" type="text" /></form>

<?php
$xml = simplexml_load_file('biblioteca_cods/cods.xml');

$search1 = $xml->xpath('lista[@type="sites de videos"]');
echo '<pre>';
print_r($search1);
echo '</pre>';
?>

////A unica coisa é que gostaria de saber como colocar uma varial no --->>> $search1 = $xml->xpath('lista[@type="VARIAVEL $_GET CATEGORIA  AQUI "]');

 

Link para o comentário
Compartilhar em outros sites

1 resposta a esta questão

Posts Recomendados

  • 0

Se você chama de categoria o <input>, então podemos chamar o valor de $categoria, e assim resolvemos a questão

$search1 = $xml->xpath($categoria);

O grande problema é o xpath, ele tem uma forma particular de codificar, ele é diferente do Basic, do Fortran, do C++, do PHP, e assim vai. Para deixar mais claro, montei um arquivo XML no astudy2.php, e o código principal no astudy.php:

astudy.php
-----------------------------------------------------
 <?php
echo "<form method=post>Código xpath <input name=categoria></form>";
echo "Exemplos de como usar o xpath:
<table>
<tr><td>/bookstore/book[1]<td>Selects the first book element that is the child of the bookstore element
<tr><td>/bookstore/book[last()]<td>Selects the last book element that is the child of the bookstore element
<tr><td>/bookstore/book[last()-1]<td>Selects the last but one book element that is the child of the bookstore element
<tr><td>/bookstore/book[position()<3]<td>Selects the first two book elements that are children of the bookstore element
<tr><td>//title[@lang]<td>Selects all the title elements that have an attribute named lang
<tr><td>//title[@lang='en']<td>Selects all the title elements that have a 'lang' attribute with a value of 'en'
<tr><td>/bookstore/book[price>35.00]<td>Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00
<tr><td>/bookstore/book[price>35.00]/title<td>Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00</table>";
if(isset($_POST['categoria']) && !empty($_POST['categoria'])){
	$categoria=$_POST['categoria'];
	$xml = simplexml_load_file('astudy2.php');
	$search1 = $xml->xpath($categoria);
	echo '<p>Resposta do xpath<pre>';
	print_r($search1);
	echo '</pre>';}
?>

astudy2.php
--------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>

<bookstore>

<book category="cooking">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="children">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

<book category="web">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>

<book category="web">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>

</bookstore>

 

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...