使用 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>
我正在使用 SimpleXML
和 simplexml_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);
}
我正在尝试解析来自 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>
我正在使用 SimpleXML
和 simplexml_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);
}