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条)

报告相同问题?

悬赏问题

  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器