Ejemplo n.º 1
2
Archivo: 040.php Proyecto: cgirl/XML
*/
//构造一个XPATH查询器
$xml = new DOMDocument('1.0', 'utf-8');
$xml->load('./037_book.xml');
$xpath = new DOMXPATH($xml);
/* $sql = ''; //路径表达式

$xpath->query($sql); */
/*
xpath的路径表达式如何写?
xpath是从根节点到某个节点经过的路径
*/
//查询034_w.xml下面的每本书的title
// 如/bookstore/book/title
$sql = '/bookstore/book/title';
$rs = $xpath->query($sql);
print_r($rs);
echo '<br/>', $rs->item(1)->nodeValue, '<br/>';
//查询book.xml下面的book节点下面的第2个title节点,哪来的第2个title节点?
/* $sql1 = '/bookstore/book/title[2]';
$rs1 = $xpath->query($sql1);
print_r($rs1->length); */
//查询bookstore下面的第2本书下面的title节点
$sql2 = '/bookstore/book[2]/title';
$rs2 = $xpath->query($sql2);
print_r($rs2);
echo '<br />', $rs2->item(0)->nodeValue, '<br />';
//查询/bookstore下面的book节点并且价格大于200元的
$sql3 = '/bookstore/book[price<20]/title';
$rs3 = $xpath->query($sql3);
print_r($rs3);
Ejemplo n.º 2
2
 /**
  * Use XPath to extract data from HTML string, and add multiple values as arrays to $college_data
  * @param [type] $attr_val [value of attribute of data]
  * @param [type] $source   [source string]
  * @param [type] $key      [key of data in JSON object]
  * @param string $attr     [type of attribute of data]
  */
 public function addMultipleValues($attr_val = null, $source = null, $key = null, $attr = "class")
 {
     global $college_data;
     $doc = new DOMDocument();
     $doc->loadHTML($source);
     $xpath = new DOMXPATH($doc);
     $values = array();
     $exp = "//*[@" . $attr . "='" . $attr_val . "']";
     $results = $xpath->query($exp);
     foreach ($results as $stat) {
         $values[] = $stat->textContent;
     }
     $college_data[$key] = $values;
 }
Ejemplo n.º 3
1
<?php

require_once 'templates/header.php';
if (isset($_POST['insert'])) {
    $xml = new DomDocument("1.0", "UTF-8");
    $xml->load('xml/furnitures.xml');
    //loading the furnitures.xml file
    $id = $_POST['id'];
    $xpath = new DOMXPATH($xml);
    foreach ($xpath->query("/furnitures/furniture[id = '{$id}']") as $node) {
        $node->parentNode->removeChild($node);
    }
    $xml->formatoutput = true;
    $xml->save('xml/furnitures.xml');
}
//loading the xml file content
$get = file_get_contents('xml/furnitures.xml');
$arr = simplexml_load_string($get);
$furnitures = $arr->furniture;
?>
<!DOCTYPE html>
<html lang="en">
			<head>
				<title>Remove Furniture</title>
				<meta charset="utf-8">
				<meta http-equiv="X-UA-Compatible" content="IE=edge">
				<meta name="viewport" content="width=device-width, initial-scale=1">
				<meta name="description" content="">
				<meta name="author" content="inventory, inventory, furniture, IMS">
				<link href="css/custom.css" rel="stylesheet"> <!-- Custom CSS -->
				<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <!-- Latest compiled and minified CSS -->
Ejemplo n.º 4
1
if (empty($word)) {
    exit('你想查啥?');
}
//解析XML并查询
$xml = new DOMDocument('1.0', 'utf-8');
$xml->load('./039_dict.xml');
$xpath = new DOMXPATH($xml);
$sql = '/dict/word[mean="' . $word . '"]/mean';
$words = $xpath->query($sql);
if ($words->length == 0) {
    echo 'sorry';
    exit;
}
//查到了
$goods = $words->item(0);
echo '货号:' . $word . '<br />';
echo '名字:', $goods->nextSibling->nodeValue, '<br />';
echo '价钱:', $goods->nextSibling->nextSibling->nodeValue, '<br />';
//////////////////////////////////////////////////////
$xml->loadhtmlfile('./039.html');
$xpath = new DOMXPATH($xml);
$sql1 = '//h2';
echo $xpath->query($sql1)->item(0)->nodeValue;
echo '<hr>';
//查询id="abc"的div节点
$sql = '//div[@id="abc"]';
echo $xpath->query($sql)->item(0)->nodeValue;
echo '<br />';
//分析第2个div下的p下的相邻span的第2个span的内容
$sql = '//div/p/span[2]';
echo $xpath->query($sql)->item(0)->nodeValue;
Ejemplo n.º 5
0
<?php

if (isset($_POST['insert'])) {
    $xml = new DomDocument("1.0", "UTF-8");
    $xml->load('studentdb.xml');
    $cname = $_POST['c_name'];
    $xpath = new DOMXPATH($xml);
    foreach ($xpath->query("/ROOT/INFO[NAME = '{$cname}']") as $node) {
        $node->parentNode->removeChild($node);
    }
    $xml->formatoutput = true;
    $xml->save('studentdb.xml');
    header("Location: http://www.yourwebsite.com/user.php");
    /* Redirect browser */
}
exit;
Ejemplo n.º 6
0
			<h1>Nos composants en tanières</h1>
		</td>	
	</tr>	
	<tr align="center">
		<td class="mh_tdpage" onClick="location.href='update.php?type=composant';" onMouseOut="this.style.backgroundColor = '#30385C';" onMouseOver="this.style.backgroundColor = '#F9BB2F';this.style.cursor='pointer'">
			<h2>Mettre à jour les composants.</h2>
		</td>		
	</tr>	
	<tr align="center">
		<td  class="mh_tdpage">
			<label for="sortilege">Sortilège :</label>
			<select id="sortilege" onChange="getComposantBySortilege();">
				<option value="0">Tous</option>
				<?php 
$xpath = new DOMXPATH(getEMRecettes());
foreach ($xpath->query("/Recettes/Recette") as $recette) {
    echo "<option value=\"" . utf8_decode($recette->getAttribute("nom")) . "\">" . utf8_decode($recette->getAttribute("nom")) . "</option>";
}
?>
			</select>
			<label for="mundidey">Mundidey :</label>
			<select id="mundidey" onChange="getComposantByMundidey();">
				<option value="0">Tous</option>
				<?php 
$xpath = new DOMXPATH(getEMRecettes());
foreach (array("du Hum ...", "du Phoenix", "de la Mouche", "du Dindon", "du Goblin", "du Démon", "de la Limace", "du Rat", "de l'Hydre", "du Ver", "du Fungus", "de la Vouivre", "du Gnu", "du Scarabée") as $mundidey) {
    echo '<option value="' . $mundidey . '">' . $mundidey . '</option>';
}
?>
			</select>			
		</td>	
Ejemplo n.º 7
-1
 /**
  *
  * @param string $expression
  * @param DOMNode $contextnode 
  * @return DOMNodeList
  */
 protected function xpath($expression, $contextnode = null)
 {
     if ($contextnode == null) {
         $queryexpression = "." . $expression;
         $contextnode = $this->resource;
     } else {
         $queryexpression = $expression;
     }
     //        echo "\n".$queryexpression;
     $nodes = $this->xpath->query($queryexpression, $contextnode);
     if ($nodes === false) {
         throw new InvalidArgumentException("Expression is malformed or the contextnode is invalid");
     }
     //        echo "-->>nodes".$nodes->length."\n";
     return $nodes;
 }