English 中文(简体)
使用XMLReader、DOM、XPath获取给定节点的标签名称和值
原标题:Get tag name and value of a given node using XMLReader, DOM, Xpath

我需要查询一个XML文档,然后显示特定的标记值,例如名字、姓氏、组(部门)、职称。

我正在使用XMLReader,因为我可能需要处理大型XML文件。我使用DomXPath来过滤数据,但是我不知道如何检索每个元素的nodeName和value。下面的代码只返回成员(member)作为节点名称?

任何帮助都将不胜感激。

<?php
    $reader = new XMLReader();
    $reader->open( include/staff.xml );

    while ($reader->read()){
        switch($reader->nodeType){
            case(XMLREADER::ELEMENT):
                if($reader->localName ===  staff ){
                    $node = $reader->expand();
                    $dom = new DomDocument();
                    $dom->formatOutput = true;
                    $n = $dom->importNode($node, true);
                    $dom->appendChild($n);
                    $xp = new DomXpath($dom);
                    $res = $xp->query("/staff/member[groups= HR ]");
                }
        }
    }
    echo $res->item(0)->nodeName;
    echo $res->item(0)->nodeValue;
?>
最佳回答

仍有些粗糙,但这是我追求的目标。我发现我的xpath查询引起了问题。

<?php
$reader = new XMLReader();
$reader->open( include/staff.xml );
$keywords =   ;
$query = "//member[groups= Research ][contains(translate(forename, ABCDEFGHIJKLMNOPQRSTUVWXYZ , abcdefghijklmnopqrstuvwxyz ) , $keywords ) or contains(translate(surname, ABCDEFGHIJKLMNOPQRSTUVWXYZ , abcdefghijklmnopqrstuvwxyz ),  $keywords )]/*";
while ($reader->read()){
    switch($reader->nodeType){
        case(XMLREADER::ELEMENT):
            if($reader->localName ===  staff ){
                $node = $reader->expand();
                $dom = new DomDocument();
                $dom->formatOutput = true;
                $n = $dom->importNode($node, true);
                $dom->appendChild($n);
                $xp = new DomXpath($dom);
                $results = $xp->query($query);
            }
    }
}
$member = array();
$staff = array();
echo $results->length;
for($i=1; $i<$results->length; $i++){
    if($results->item($i)->nodeName !==  id ){
        $member[$results->item($i)->nodeName] = $results->item($i)->nodeValue;
    }else{
        array_push($staff, $member);
    }
}
array_push($staff, $member);
var_dump($staff);

Sorry, there is no text provided to translate into Chinese. Please provide the text that you want to be translated.

问题回答

尝试 (Chángshì)

$reader->name

AND: 和

$reader->value

They supposed to be "The qualified name of the node" AND: 和 "The text value of the node" according to this page http://www.php-editors.com/php_manual/ref.xmlreader.html

很明显人们是这样使用它的:http://www.google.com/codesearch/p?hl=pl#_rn0kgFhkQA/redir/docvert/60463/url_tgz/docvert-3.2.3.tar.gz%7CP-uLGAoGHyM/docvert/core/process/ParseOpenDocument.php&q=xmlreader%20file:%5C.php$

这里可以找到更多的使用实例:。 http://www.google.com/codesearch?q=xmlreader+file:php





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签