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


Problems parsing xml with PHP and SimpleXML

我正在尝试解析来自Ontraport API的响应,该响应以丑陋的XML格式返回。

<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>

我正在使用SimpleXMLsimplexml_load_string函数。当我var_dump该函数的响应时,我得到以下输出:

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"
          }
        }
      }
    }
  }
}

我如何从那里检索公司和电子邮件值或任何特定字段,我不知道这些字段是空的还是有值的。

我没有看到那些有值的字段名称,我不能假设字段顺序。

编辑:我不认为这是@Rizier123所说的重复,因为我正在尝试根据元素属性检索项目,我什至没有var_dump具有值的字段的属性。因此,针对另一个问题提出和接受的解决方案在这里不适用。正如他所问的,我正在添加我用来测试它的完整和真实的代码:

$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);

正如@luciano评论中建议的那样,我使用 $field[n]->attributes() 修复了遍历 field 数组的问题

当您使用 print_r()var_dump() 或任何尝试将值转换为字符串的函数时,SimpleXML 不会同时显示任何元素的属性和文本。SimpleXML 的get_properties处理程序存在于一些方面,例如,将@attributes显示为属性,尽管它不可访问。

从元素中检索属性和文本的唯一方法是遍历它并使用$field->attributes()和$field转换为字符串手动请求每个属性和文本。

我的案例的最终代码是:

$responseData = simplexml_load_string($response);
$fields = $responseData->contact[0]->Group_Tag->field;
foreach ($fields as $field) {
    printf("%s : %s 'n'r",
        $field->attributes()->name,
        $field);
}