2013-10-18 51 views
0

我需要得到一个属性:如何使用php获取xml属性?

<Info InstallPath="C:\Program Files\MyProg" Version="1.0" > 

    <Modules> 
     <parser.exe Launched="Off" /> 
     <analyzer.exe Launched="On" /> 
    </Modules> 

</Info> 

我写道:

echo $Parser = $xml->Info->Modules->parser.exe->attributes()->Launched; 

但它不工作,因为 “parser.exe” 包含一个圆点

Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';'

+1

顺便说一句,这个问题是相当普遍的初学者,它是一样的破折号:http://stackoverflow.com/questions/9951753/php-simplexml-load-file-with-a-dash - 错误写出根元素的名称也是一个常见的错误。所有这些问题都得到了很好的解释,并且在http://php.net/simplexml.examples-basic(示例#2和#3)中进行了很好的说明。 – hakre

回答

1

如果<Info>是你的根元素然后:

echo $xml->Modules->{'parser.exe'}->attributes()->Launched; 

因为在你<parser.exe>元素的.字符,你需要使用{}语法来访问属性。

在SimpleXML中,对象($xml)引用根元素,所以$xml->Info不是必需的。