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 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?