dsjswclzh40259075 2016-10-04 23:06
浏览 60

使用搜索进行php和xml解析

this is my xml file and my php code. i want to search by firstname via a textbox in my php form and i want to display all the info about the specific student, but the issue is that my xpath is not working. any help with the 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>
  • 写回答

1条回答 默认 最新

  • dongrong5189 2016-10-04 23:30
    关注

    Drop the last forward slash of your query.
    Change from this:

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

    to this:

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

    WORKING EXAMPLE:

    <?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);
    

    DEMO:

    http://ideone.com/Z23XQY

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题