dongsou0083 2015-03-09 20:52
浏览 53
已采纳

使用PHP和SimpleXML解析xml时出现问题

I'm trying to parse a response from the Ontraport API, which is returned in an ugly XML format.

<result>
    <contact id="1" date="1424746532" dlm="1425357692" score="0.00" purl="" bulk_mail="1">
        <Group_Tag name="Contact Information">
            <field name="Company">Test.com</field>
            <field name="Email">test@test.com</field>
            <field name="Group"/>
            <field name="Specialty"/>
            <field name="User ID"/>
            <field name="Display First"/>
            <field name="Display Last"/>
        </Group_Tag>
    </contact>
</result>

Im using SimpleXML and the simplexml_load_string function. When I var_dump the response from that function I get the following output:

object(SimpleXMLElement)#1 (1) {
  ["contact"]=>
  object(SimpleXMLElement)#2 (2) {
    ["@attributes"]=>
    array(6) {
      ["id"]=>
      string(1) "1"
      ["date"]=>
      string(10) "1424746532"
      ["dlm"]=>
      string(10) "1425357692"
      ["score"]=>
      string(4) "0.00"
      ["purl"]=>
      string(0) ""
      ["bulk_mail"]=>
      string(1) "1"
    }
    ["Group_Tag"]=>
    object(SimpleXMLElement)#3 (2) {
      ["@attributes"]=>
      array(1) {
        ["name"]=>
        string(19) "Contact Information"
      }
      ["field"]=>
      array(7) {
        [0]=>
        string(8) "Test.com"
        [1]=>
        string(13) "test@test.com"
        [2]=>
        object(SimpleXMLElement)#4 (1) {
          ["@attributes"]=>
          array(1) {
            ["name"]=>
            string(5) "Group"
          }
        }
        [3]=>
        object(SimpleXMLElement)#5 (1) {
          ["@attributes"]=>
          array(1) {
            ["name"]=>
            string(9) "Specialty"
          }
        }
        [4]=>
        object(SimpleXMLElement)#6 (1) {
          ["@attributes"]=>
          array(1) {
            ["name"]=>
            string(7) "User ID"
          }
        }
        [5]=>
        object(SimpleXMLElement)#7 (1) {
          ["@attributes"]=>
          array(1) {
            ["name"]=>
            string(13) "Display First"
          }
        }
        [6]=>
        object(SimpleXMLElement)#8 (1) {
          ["@attributes"]=>
          array(1) {
            ["name"]=>
            string(12) "Display Last"
          }
        }
      }
    }
  }
}

How could I retrieve the Company and Email values, or any specific field from there, which I don't know if is empty or has a value.

I don't see the field name for those with a value and I can't assume the fields order.

Edit: I don't think this is a duplicate as @Rizier123 said, as I'm trying to retrieve an item based on an element attribute, and I'm not even getting on the var_dump the attributes for the fields that have a value. Thus, the proposed and accepted solutions for that other question don't apply here. As he asked, I'm adding the full and real code that I'm using to test this:

$response = '<result>
    <contact id="1" date="1424746532" dlm="1425357692" score="0.00" purl="" bulk_mail="1">
        <Group_Tag name="Contact Information">
            <field name="Company">Test.com</field>
            <field name="Email">test@test.com</field>
            <field name="Group"/>
            <field name="Specialty"/>
            <field name="User ID"/>
            <field name="Display First"/>
            <field name="Display Last"/>
        </Group_Tag>
    </contact>
</result>';
$responseData = simplexml_load_string($response);
var_dump($responseData);
  • 写回答

1条回答 默认 最新

  • dplsnw7329 2015-03-09 22:16
    关注

    As @luciano suggested on a comment, I fixed this iterating through the field array using $field[n]->attributes()

    SimpleXML doesn't shows the attributes and text at the same time for any element when you're using print_r(), var_dump() or any function that tries to cast the values to a string. The get_properties handler for SimpleXML lies in a few things, for example, showing @attributes as a property although it isn't accessible.

    The only way to retrieve both the attributes and text from an element, is iterating through it and manually asking for each using $field->attributes() and $field casted as string.

    The final code for my case was:

    $responseData = simplexml_load_string($response);
    $fields = $responseData->contact[0]->Group_Tag->field;
    foreach ($fields as $field) {
        printf("%s : %s 
    ",
            $field->attributes()->name,
            $field);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器