duanaiguang1960 2014-01-17 23:31
浏览 53
已采纳

如何从xml中提取值

I'm new to PHP. I'm trying to get the data out of the below XML. Now, in my code $data->Address contains value of the below code i.e:

$data->Address = "<tolist></tolist>
        <cclist>
            <cc>
                <contactpersonname>niraj</contactpersonname>
                <name>niraj</name>
                <email>stgh@gmail.com</email>
                <number>+91.3212365212</number>
                <prefix>Ms.</prefix>
                <contactpersonprefix>Ms.</contactpersonprefix>
            </cc>
            <cc>
                <contactpersonname>fdg</contactpersonname>
                <name>admin</name>
                <email>admin12@gmail.com</email>
                <number>+91.4554343234</number>
                <prefix>Mr.</prefix>
                <contactpersonprefix>Mr.</contactpersonprefix>
            </cc>
        </cclist>";

Now I want to extract the <contactpersonname> tag and print it. How can I do this?

  • 写回答

4条回答 默认 最新

  • dougongnan2167 2014-01-18 00:02
    关注

    Since your XML is missing a tag that encompasses all others, you need to create on in order to get parsers to work properly:

    <?php
    $buffer = "<tolist></tolist>
            <cclist>
                <cc>
                    <contactpersonname>niraj</contactpersonname>
                    <name>niraj</name>
                    <email>stgh@gmail.com</email>
                    <number>+91.3212365212</number>
                    <prefix>Ms.</prefix>
                    <contactpersonprefix>Ms.</contactpersonprefix>
                </cc>
                <cc>
                    <contactpersonname>fdg</contactpersonname>
                    <name>admin</name>
                    <email>admin12@gmail.com</email>
                    <number>+91.4554343234</number>
                    <prefix>Mr.</prefix>
                    <contactpersonprefix>Mr.</contactpersonprefix>
                </cc>
            </cclist>";
    
    //   ***** wrap the whole thing in a <root> tag...
    $xml   = simplexml_load_string("<root>".$buffer."</root>");
    $array = json_decode(json_encode((array) $xml), 1);
    
    echo "<pre>";
    print_r($array);
    echo "</pre>";
    ?>
    

    Result:

    Array
    (
        [tolist] => Array
            (
            )
    
        [cclist] => Array
            (
                [cc] => Array
                    (
                        [0] => Array
                            (
                                [contactpersonname] => niraj
                                [name] => niraj
                                [email] => stgh@gmail.com
                                [number] => +91.3212365212
                                [prefix] => Ms.
                                [contactpersonprefix] => Ms.
                            )
    
                        [1] => Array
                            (
                                [contactpersonname] => fdg
                                [name] => admin
                                [email] => admin12@gmail.com
                                [number] => +91.4554343234
                                [prefix] => Mr.
                                [contactpersonprefix] => Mr.
                            )
    
                    )
    
            )
    
    )
    

    UPDATED

    Now you can navigate down to where you want to go with

    echo "<pre>";
    $ccList = $array['cclist'];
    $cc = $ccList['cc'];
    $contacts = array();
    foreach($cc as $i=>$val) {
      $contacts[$i]=$val['contactpersonname'];
    }
    echo "first contact: " . $contacts[0] . "<br>";
    echo "second contact: " . $contacts[1] ."<br>";
    

    Result:

    first contact: niraj
    second contact: fdg
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了