开发者

Can't really understand children() in simpleXML

开发者 https://www.devze.com 2023-03-26 03:31 出处:网络
I am working on a simple opencalais project and i needed my program to dynamically realize the categories children count.

I am working on a simple opencalais project and i needed my program to dynamically realize the categories children count.

I was going to write a separate function which would handle teh subchildren if they occured like they have in the example. ( multiple companies/persons/etc) but it seems that this code took care of it all. Well it's a really good thing since it saved me some work.But i am new to PHP and i have read through the children() documentation but cant understand how this happened.

The Code :

$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";
    }
}

The 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
          开发者_StackOverflow社区  [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


look at your xml file but understand how arrays work also

what is happening is that the data is being grouped into arrays within the 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/>'; 

 }

[Company][0] [Event][0]

[Company][1] [Event][1]

and so on

0

精彩评论

暂无评论...
验证码 换一张
取 消