drw85135 2014-08-04 16:26
浏览 17
已采纳

使用SimpleXML和PHP不能拉属性

I am pulling values from an API using simple XML. Earlier calls worked fine but for the life of me I cannot get this last piece to pull data. I am making the call with:

$postq='https://api.eveonline.com/char/SkillQueue.xml.aspx?keyid='.$key.'&vCode='.$code;
$qpull = simplexml_load_file($postq);

An example of the expected output is:

<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="2">
  <currentTime>2009-03-18 13:19:43</currentTime>
  <result>
    <rowset name="skillqueue" key="queuePosition" columns="queuePosition,typeID,level,startSP,endSP,startTime,endTime">
      <row queuePosition="1" typeID="11441" level="3" startSP="7072" endSP="40000" startTime="2009-03-18 02:01:06" endTime="2009-03-18 15:19:21" />
      <row queuePosition="2" typeID="20533" level="4" startSP="112000" endSP="633542" startTime="2009-03-18 15:19:21" endTime="2009-03-30 03:16:14" />
    </rowset>
  </result>
  <cachedUntil>2009-03-18 13:34:43</cachedUntil>
</eveapi>

The call works and I am currently at the following:

  foreach( $qpull -> result as $c1)
    {
        foreach( $c1 -> rowset as $c2)
        {

            foreach( $c2 -> row as $c3)
            {

                var_dump($c3);
            }
        }
    }

This may be over complicating how it needs to be done but this is after hours of trying different ways to do it.

This is an example of the output:

    object(SimpleXMLElement)[20]
  public '@attributes' => 
    array (size=7)
      'queuePosition' => string '0' (length=1)
      'typeID' => string '3425' (length=4)
      'level' => string '1' (length=1)
      'startSP' => string '0' (length=1)
      'endSP' => string '500' (length=3)
      'startTime' => string '2014-08-04 16:16:11' (length=19)
      'endTime' => string '2014-08-04 16:30:16' (length=19)
object(SimpleXMLElement)[21]
  public '@attributes' => 
    array (size=7)
      'queuePosition' => string '1' (length=1)
      'typeID' => string '11566' (length=5)
      'level' => string '1' (length=1)
      'startSP' => string '0' (length=1)
      'endSP' => string '500' (length=3)
      'startTime' => string '2014-08-04 16:30:16' (length=19)
      'endTime' => string '2014-08-04 16:44:21' (length=19)

However if I try to select a value using:

var_dump($c3->level);

I am just getting blanks and cannot work out what I am doing wrong.

Thanks.

  • 写回答

3条回答 默认 最新

  • dongmeng9048 2014-08-04 16:31
    关注

    For attributes, use the attributes method: SimpleXMLElement::attributes

    Specifically the following:

    <?php
    include 'example.php';
    
    $movies = new SimpleXMLElement($xmlstr);
    
    /* Access the <rating> nodes of the first movie.
     * Output the rating scale, too. */
    foreach ($movies->movie[0]->rating as $rating) {
        switch((string) $rating['type']) { // Get attributes as element indices
        case 'thumbs':
            echo $rating, ' thumbs up';
            break;
        case 'stars':
            echo $rating, ' stars';
            break;
        }
    }
    

    For the example:

    <?php
    $xmlstr = <<<XML
    <?xml version='1.0' standalone='yes'?>
    <movies>
     <movie>
      <title>PHP: Behind the Parser</title>
      <characters>
       <character>
        <name>Ms. Coder</name>
        <actor>Onlivia Actora</actor>
       </character>
       <character>
        <name>Mr. Coder</name>
        <actor>El Act&#211;r</actor>
       </character>
      </characters>
      <plot>
       So, this language. It's like, a programming language. Or is it a
       scripting language? All is revealed in this thrilling horror spoof
       of a documentary.
      </plot>
      <great-lines>
       <line>PHP solves all my web problems</line>
      </great-lines>
      <rating type="thumbs">7</rating>
      <rating type="stars">5</rating>
     </movie>
    </movies>
    XML;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序