[英]Parse XML using PHP
I am using SimpleXMLElement
to parse xml in PHP and it is working fine. 我正在使用SimpleXMLElement
来解析PHP中的xml,它运行正常。 My xml is like, 我的xml就像,
<?xml version="1.0" encoding="ISO-8859-1"?>
<app id="123">
<username>sample</username>
</app>
My code is, 我的代码是,
$xml = new SimpleXMLElement(file_get_contents(file.xml));
foreach($xml->App as $product)
{
$user = $product->username;
}
It gives correct username "sample" as output. 它提供正确的用户名“sample”作为输出。
But now I need to get value of id
inside the app tag. 但现在我需要在app标签中获取id
值。 If I changes the xml format and gives the id inside <app>
tag as <id>123</id>
, I can get it simply. 如果我更改了xml格式并将<app>
标记内的id作为<id>123</id>
,我可以简单地得到它。
But I cant change the format! 但我不能改变格式! Is there a way to get the id
value within the same format? 有没有办法让id
值在同一格式内?
try this: 尝试这个:
$xml = new SimpleXMLElement(file_get_contents(file.xml));
foreach($xml->App as $product)
{
$user = $product->username;
$id = $product->attributes()->id;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.