71069

Can't really understand children() in simpleXML

Question:

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 [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

Answer1:

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

Recommend

  • making my tabular form dynamic
  • Dynamically generated radiobutton by jQuery cannot be selected in IE6
  • 3D transforms (translate3D) appear to be causing sluggish jQuery animations on mobile devices
  • Projects fail to start in iOS when adding XSuperObject
  • Web Scraping multiple Links using R
  • convert a string to xml doc with jquery or the other way round
  • how to get the selected value of dropdown list of asp.net and store in session variable?
  • making the background translucent
  • Kafka topic deletion not working
  • Opa: Iterating through stringmap and forming a new string based on it
  • Problem serializing DecimalFormatSymbols in 2.3.4
  • Does Java EE 6 framework only for Web Application Or can I use it for Client Application as well
  • Issue with static code in Android
  • Accessing music files into my application programatically
  • Zend Framework 2, Module Redirect
  • Why does PHP appear to evaluate this condition incorrectly?
  • Contact form problem - I do receive messages, but no contents (blank page)
  • Problem with rejecting incoming call
  • Doctrine/Symfony entity generator and generating entity from one table
  • Why cepheus don't send int without quotes to orion?
  • GAE: Way to get reference to an HttpSession from its ID?
  • Force show.bind execution
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Why is an OPTIONS request sent to the server?
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • How to add date and time under each post in guestbook in google app engine
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Is there a mandatory requirement to switch app.yaml?
  • File upload with ng-file-upload throwing error
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • AngularJs get employee from factory
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • To Get the radio button value in ruby on rails
  • java string with new operator and a literal