尝试获取非对象-> xml的属性

时间:2022-04-03 15:42:28

I'm retrieving an xml:

我检索xml:

$xml = file_get_contents($query);

echo $xml->Email;
echo $xml[0]->Email;

The xml (echo $xml) looks like this:

xml (echo $xml)如下:

<GetUserInfo>
<Customer>TestUser</Customer>
<Email>test@test.com</Balance>
</GetUserInfo>

But both those approaches give the following error:

但这两种方法都存在以下错误:

Notice: Trying to get property of non-object in test.php on line 86

Notice: Trying to get property of non-object in test.php on line 87

How can I get the value of Email and Customer?

如何获得电子邮件和客户的价值?

2 个解决方案

#1


3  

file_get_contents() returns the file content, not an object. You can only use preg_match if you want to stick to string content (totally not advised):

file_get_contents()返回文件内容,而不是对象。如果您希望坚持字符串内容(完全不建议使用),您只能使用preg_match:

preg_match('~<Email>([^<]+)</Email>~i', file_get_contents($__filePath__), $emails);

I recommend using DOMDocument and DOMXPath (code not tested):

我建议使用DOMDocument和DOMXPath(代码未测试):

$XMLDoc = new DOMDocument();
$XMLDoc->load($__filePath__);
$XPath = new DOMXPath($XMLDoc);
$emails = $XPath->query('//email');
foreach ($emails as $email)
    var_dump($email->nodeValue);

You might use another Xpath expression like //email[1] or /GetUserInfo/Email The foreach may also be replaced by $email = reset($emails); if you only want the first mail.

您可能会使用另一个Xpath表达式,例如//email[1]或/GetUserInfo/电子邮件,foreach也可以被$email = reset($email)代替;如果你只想要第一封邮件。

#2


3  

Your $xml is a string. $xml-> accesses a property of an object. That is not compatible. A php string is not an object.

您的$xml是一个字符串。$xml->访问对象的属性。这是不兼容的。php字符串不是对象。

You may want to use var_dump() instead of echo() to see all the details of your variables.

您可能想使用var_dump()而不是echo()来查看变量的所有细节。

A simple string to object convertor is simplexml_load_string()

对象转换器的简单字符串是simplexml_load_string()

$xml='
<GetUserInfo>
<Customer>TestUser</Customer>
<Email>test@test.com</Email>
</GetUserInfo>
';

var_dump($xml);
$Xml = simplexml_load_string($xml);
var_dump($Xml);
echo($Xml->Email);

#1


3  

file_get_contents() returns the file content, not an object. You can only use preg_match if you want to stick to string content (totally not advised):

file_get_contents()返回文件内容,而不是对象。如果您希望坚持字符串内容(完全不建议使用),您只能使用preg_match:

preg_match('~<Email>([^<]+)</Email>~i', file_get_contents($__filePath__), $emails);

I recommend using DOMDocument and DOMXPath (code not tested):

我建议使用DOMDocument和DOMXPath(代码未测试):

$XMLDoc = new DOMDocument();
$XMLDoc->load($__filePath__);
$XPath = new DOMXPath($XMLDoc);
$emails = $XPath->query('//email');
foreach ($emails as $email)
    var_dump($email->nodeValue);

You might use another Xpath expression like //email[1] or /GetUserInfo/Email The foreach may also be replaced by $email = reset($emails); if you only want the first mail.

您可能会使用另一个Xpath表达式,例如//email[1]或/GetUserInfo/电子邮件,foreach也可以被$email = reset($email)代替;如果你只想要第一封邮件。

#2


3  

Your $xml is a string. $xml-> accesses a property of an object. That is not compatible. A php string is not an object.

您的$xml是一个字符串。$xml->访问对象的属性。这是不兼容的。php字符串不是对象。

You may want to use var_dump() instead of echo() to see all the details of your variables.

您可能想使用var_dump()而不是echo()来查看变量的所有细节。

A simple string to object convertor is simplexml_load_string()

对象转换器的简单字符串是simplexml_load_string()

$xml='
<GetUserInfo>
<Customer>TestUser</Customer>
<Email>test@test.com</Email>
</GetUserInfo>
';

var_dump($xml);
$Xml = simplexml_load_string($xml);
var_dump($Xml);
echo($Xml->Email);