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 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色