无法真正理解simpleXML中的children()

时间:2011-08-03 15:38:19

标签: php xml

我正在开发一个简单的opencalais项目,我需要我的程序来动态实现孩子们的数量。

我打算写一个单独的函数来处理子孙,如果它们像在示例中那样发生。 (多个公司/人/等)但似乎这个代码照顾了一切。好吧,这是一件非常好的事情,因为它为我节省了一些工作。但我是PHP的新手,我已经阅读了孩子们()文档,但无法理解这是怎么发生的。

守则:

$xml = simplexml_load_string("$response");
print_fr($xml);

$categories = $xml->CalaisSimpleOutputFormat->children();
print_fr($categories);

foreach( $categories as $entities ) {
    $eName = $entities->getName();
    if($eName!== "Topics") {
        echo "<br /> The Entity is -- \" ".$eName."\"";
        echo "<br /> The Entity Name is -- \" ".$entities."\"";
        echo "<br /> The Relevance Score is -- \" ".$entities['relevance']."\"";
    }
    else if($eName == "Topics") {
        echo "Deal with topics later";
    }
}

O / P:

SimpleXMLElement Object
(
    [Description] => SimpleXMLElement Object
        (
            [allowDistribution] => false
            [allowSearch] => false
            [calaisRequestID] => a647072b-203c-cbb0-1319-045ba23774d2
            [externalID] => SimpleXMLElement Object
                (
                    [0] =>  
                )

            [id] => http://id.opencalais.com/ZwD-c-aYZMsFWaSXn2vWbw
            [about] => http://d.opencalais.com/dochash-1/e12aabaf-1b01-3844-96bc-90238dae24dc
            [docTitle] => SimpleXMLElement Object
                (
                )

            [docDate] => 2007-09-15
            [externalMetadata] => SimpleXMLElement Object
                (
                )

            [submitter] => Calais REST Sample
        )

    [CalaisSimpleOutputFormat] => SimpleXMLElement Object
        (
            [Company] => Array
                (
                    [0] => Tensleep Corporation
                    [1] => International Star Inc.
                    [2] => XSTV Media Inc.
                    [3] => XSTV Corporation
                    [4] => Gerard Klauer Mattison
                    [5] => IDC
                )

            [Event] => Array
                (
                    [0] => General or Shareholder Meeting
                    [1] => M&A
                )

            [Position] => analyst
            [City] => Shreveport
            [Facility] => The Hilton Hotel
            [Person] => Array
                (
                    [0] => David Bailey
                    [1] => Roger Kay
                )

            [Topics] => SimpleXMLElement Object
                (
                    [Topic] => Business_Finance
                )

        )

)

SimpleXMLElement Object
(
    [Company] => Array
        (
            [0] => Tensleep Corporation
            [1] => International Star Inc.
            [2] => XSTV Media Inc.
            [3] => XSTV Corporation
            [4] => Gerard Klauer Mattison
            [5] => IDC
        )

    [Event] => Array
        (
            [0] => General or Shareholder Meeting
            [1] => M&A
        )

    [Position] => analyst
    [City] => Shreveport
    [Facility] => The Hilton Hotel
    [Person] => Array
        (
            [0] => David Bailey
            [1] => Roger Kay
        )

    [Topics] => SimpleXMLElement Object
        (
            [Topic] => Business_Finance
        )

)


The Entity is -- " Company"
The Entity Name is -- " Tensleep Corporation"
The Relevance Score is -- " 0.451"
The Entity is -- " Company"
The Entity Name is -- " International Star Inc."
The Relevance Score is -- " 0.142"
The Entity is -- " Company"
The Entity Name is -- " XSTV Media Inc."
The Relevance Score is -- " 0.364"
The Entity is -- " Event"
The Entity Name is -- " General or Shareholder Meeting"
The Relevance Score is -- " "
The Entity is -- " Position"
The Entity Name is -- " analyst"
The Relevance Score is -- " 0.334"
The Entity is -- " City"
The Entity Name is -- " Shreveport"
The Relevance Score is -- " 0.112"
The Entity is -- " Company"
The Entity Name is -- " XSTV Corporation"
The Relevance Score is -- " 0.319"
The Entity is -- " Company"
The Entity Name is -- " Gerard Klauer Mattison"
The Relevance Score is -- " 0.247"
The Entity is -- " Company"
The Entity Name is -- " IDC"
The Relevance Score is -- " 0.169"
The Entity is -- " Event"
The Entity Name is -- " M&A"
The Relevance Score is -- " "
The Entity is -- " Facility"
The Entity Name is -- " The Hilton Hotel"
The Relevance Score is -- " 0.112"
The Entity is -- " Person"
The Entity Name is -- " David Bailey"
The Relevance Score is -- " 0.247"
The Entity is -- " Person"
The Entity Name is -- " Roger Kay"
The Relevance Score is -- " 0.169"
Deal with topics later

1 个答案:

答案 0 :(得分:0)

查看您的xml文件,但了解数组的工作原理

正在发生的事情是数据被分组到CalaisSimpleOutputFormat中的数组

    $xml = simplexml_load_string('
<CalaisSimpleOutputFormat> 
   <Company>company 1</Company> 
   <event>event 1</event> 
   <Company>company 2</Company> 
   <event>event 2</event> 
 </CalaisSimpleOutputFormat> 
 ');

$CalaisSimpleOutputFormat = $xml->children();

foreach($xml  as $cat_name=>$data_in_cat ){ 

   echo $cat_name.' '.$data_in_cat.'<br/>'; 

 }

[公司] [0] [事件] [0]

[公司] [1] [事件] [1]

等等