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);
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题