dougu2036 2018-03-22 22:31
浏览 39
已采纳

PHP SimpleXML - 获得价值

I am trying to use SimpleXML with PHP to get a value from XML stored in a database. A portion of my XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0_161" class="java.beans.XMLDecoder">
  <object class="atavism.agis.objects.AgisMob" id="AgisMob0">
  <void property="name">
    <string>Rachels</string>
  </void>
  <void property="oid">
  <object class="atavism.server.engine.OID" id="OID0">
  <void property="data">
    <long>41321</long>
  </void>

I also have other XML in the same "document" that looks like this:

So that being said, I would probably need to fetch it where the property attribute is equal to "name". I do plan on getting other information from this document later on as well, just need to figure out how.

Here is the code I have so far:

$blob_query = mysqli_query($dbh8, "SELECT * FROM objstore WHERE obj_id='$characterId' AND type='PLAYER' AND namespace_int='3' ORDER BY obj_id DESC LIMIT 1");           
        if($blob_query){
            $blob_row = mysqli_fetch_assoc($blob_query);
            $data = $blob_row['data'];

            $xml=simplexml_load_string($data) or die("Error: Cannot create object");
            echo $xml->void[0]->string;

        }

It is not displaying anything. It should be displaying "Rachels". Any help is very much appreciated!

  • 写回答

2条回答 默认 最新

  • doupafu6980 2018-03-22 23:25
    关注

    You're missing the <object> tag to get <string> tag content.

    Assuming you XML look likes (as a valid XML):

    $data = '<?xml version="1.0" encoding="UTF-8"?>
    <java version="1.8.0_161" class="java.beans.XMLDecoder">
      <object class="atavism.agis.objects.AgisMob" id="AgisMob0">
          <void property="name">
            <string>Rachels</string>
          </void>
      </object>
      <void property="oid">
          <object class="atavism.server.engine.OID" id="OID0">
              <void property="data">
                <long>41321</long>
              </void>
          </object>
      </void>
    </java>';
    

    You have to use:

    $xml=simplexml_load_string($data) or die("Error: Cannot create object");
    echo $xml->object->void[0]->string;
    

    outputs:

    Rachels
    

    Note also that [0] is not necessary (in this case):

    echo $xml->object->void->string;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ros小车启动launch文件报错
  • ¥15 vs2015到期想登陆但是登陆不上
  • ¥15 求解四个变换矩阵中的未知数θ
  • ¥20 Intellij实现web登录界面
  • ¥15 IPQ5018制作烧录固件,boot运行失败(操作系统-linux)(相关搜索:操作系统)(相关搜索:操作系统)
  • ¥20 icefall在librispeech基础上加入个人数据集
  • ¥30 keepalive高可用故障运维配置询问
  • ¥15 求帮助!国家电网内网u盘突然识别不出来了。
  • ¥15 matlab语音变速变调同时实现
  • ¥15 如何用Thoony写ESP32温湿度检测无源蜂鸣器报警代码?