Я работаю над простым проектом opencalais, и мне нужна моя программа для динамического осознания категорий детей.
Я собирался написать отдельную функцию, которая обрабатывала бы дочерних, если бы они происходили так, как они есть в этом примере. (несколько компаний / лиц / и т. д.), но, похоже, этот код позаботился обо всем этом. Ну, это действительно хорошо, так как это сэкономило мне некоторую работу. Но я новичок в PHP, и я прочитал документацию children (), но не могу понять, как это произошло.
Код :
$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
посмотрите на свой 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]
и так далее