【问题标题】:php and xml parsing using search使用搜索解析 php 和 xml
【发布时间】:2016-10-04 23:06:49
【问题描述】:

这是我的 xml 文件和我的 php 代码。我想通过我的 php 表单中的文本框按名字搜索,我想显示有关特定学生的所有信息,但问题是我的 xpath 不起作用。任何有关 xpath 的帮助。

<students>
<student>
        <firstname>John</firstname>
        <lasttname>Snow</lasttname>
        <student_id>160600</student_id>
        <gender>male</gender>
        <dob>23-06-95</dob>
        <age>21</age>
        <email>JohnSnow@gmail.com</email>
        <mobilenumber>57675060</mobilenumber>
        <address>Winter Fel</address>
        <cohort>BSE15PT</cohort>
        <programme>Software Engineering</programme>
        <mode>PT</mode>
    </student> 
    <student>
        <firstname>meryl</firstname>
        <lastname>Burton</lastname>
        <student_id>150500</student_id>
        <gender>female</gender>
        <dob>26-07-95</dob>
        <email>mirena@gmail.com</email>
        <mobilenumber>57800603</mobilenumber>
        <address>rose hill</address>
        <cohort>BSE15AFT</cohort>
        <programme>software engineering</programme>
        <mode>ft</mode>
    </student>
</students>  


<?php
    if(isset($_POST['search']))
    {
        $xml=simplexml_load_file("studentInstance.xml") or die("Error: Cannot Create Object");
        $xpath = $xml;
        //query the document
        $name = $_POST['studentname'];

    $query = $xpath->query("/students/student/[firstname = '$name']");
        echo $query;
    }

    ?>
    <!DOCTYPE html>
    <html>
        <head>
            <title>Searching</title>
        </head>
        <body>
        <form method="POST" action="searchRecord.php">
            <label>Enter Student Name</label>
             <input type="text" name="studentname"><br>
            <input type="submit" name="search" value="search">
            </form>
        </body>
    </html>

【问题讨论】:

  • 我不确定,我在手机里,但学生是一个列表,我认为您可能需要特定索引 /students/student[1]/firstname

标签: php xml forms xpath xml-parsing


【解决方案1】:

删除查询的最后一个正斜杠。
从此改变:

"/students/student/[firstname = '$name']"

到这里:

"/students/student[firstname = '$name']"

工作示例:

<?php

$xml = <<< EOF
<students>
<student>
        <firstname>John</firstname>
        <lasttname>Snow</lasttname>
        <student_id>160600</student_id>
        <gender>male</gender>
        <dob>23-06-95</dob>
        <age>21</age>
        <email>JohnSnow@gmail.com</email>
        <mobilenumber>57675060</mobilenumber>
        <address>Winter Fel</address>
        <cohort>BSE15PT</cohort>
        <programme>Software Engineering</programme>
        <mode>PT</mode>
    </student> 
    <student>
        <firstname>meryl</firstname>
        <lastname>Burton</lastname>
        <student_id>150500</student_id>
        <gender>female</gender>
        <dob>26-07-95</dob>
        <email>mirena@gmail.com</email>
        <mobilenumber>57800603</mobilenumber>
        <address>rose hill</address>
        <cohort>BSE15AFT</cohort>
        <programme>software engineering</programme>
        <mode>ft</mode>
    </student>
</students> 
EOF;
$xml = simplexml_load_string($xml);
$names = $xml->xpath("/students/student[firstname = 'meryl']");
print_r($names);

演示:

http://ideone.com/Z23XQY

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多