SimpleXMLElement对象,其中包含@和破折号

时间:2014-01-29 10:38:53

标签: php simplexml

我有SimpleXMLElement对象看起来像这样

    SimpleXMLElement Object (
     [report-name] = SimpleXMLElement Object
         (
         [@attributes] = Array
             (
             [name] = Raport
             )

         )

     [date-range] = SimpleXMLElement Object
         (
         [@attributes] = Array
             (
             [date] = Jan 29, 2014
             )

         ) )

如何从中获取'日期'值?

2 个答案:

答案 0 :(得分:2)

来源:PHP websiteW3 Schools website

对于简单XML对象的基本用法,请参阅W3学校的链接。

要使用连字符解决您的问题,请参阅直接来自PHP网站的此示例:

$xml = simplexml_load_string($input);
$callback = $xml->{"callback-url"};

答案 1 :(得分:0)

连字符( - )是问题,但你有孩子的方法,这将有助于你。

为了更好的理解

<?php

////xml which contains the hyphen
$string = <<<XML
<a>
 <x-y name="one" game="lonely">1</x-y>
</a>
XML;

//load XML object
$xml = simplexml_load_string($string);

//process each children
foreach($xml->children() as $a) 
{   
    //collect the attributes and process it
    foreach($a->attributes() as $b => $c) 
    {
        //name and value of attribute
        echo $b,'="',$c,"\"\n";

    }
}