How to skip a SimpleXML object in PHP?

I have a simpleXml object and you want to read data from the object, I am new to PHP and do not know how to do this. Details of the facility are as follows.

I need to read [description] and [clock]. Thankyou.

SimpleXMLElement Object ( [@attributes] => Array ( [type] => array ) [time-entry] => Array ( [0] => SimpleXMLElement Object ( [date] => 2010-01-26 [description] => TCDM1 data management: sort & upload [hours] => 1.0 [id] => 21753865 [person-id] => 350501 [project-id] => 4287373 [todo-item-id] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => integer [nil] => true ) ) ) [1] => SimpleXMLElement Object ( [date] => 2010-01-27 [description] => PDCH1: HTML [hours] => 0.25 [id] => 21782012 [person-id] => 1828493 [project-id] => 4249185 [todo-item-id] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => integer [nil] => true ) ) ) ) ) 

Please help me. I try a lot of things, but don't get the syntax correctly.

+6
php simplexml
source share
3 answers

It is very difficult to read above, if you could give us a screenshot or give the line spacing and indentation, that would be a little better if you have an object

$ xml, which is a simplexml object, you can access elements of type

 $xml->element. 

This seems to be a time entry element, in which case you will scroll like this:

 foreach( $xml->{'time-entry'} as $time_entry ) { echo $time_entry->description . "<br />\n"; } 
+10
source share

If the object that you print_r -ed in the question is in $element , then the next two loops will cyclically intersect with the two time-entry elements when printing their corresponding description and hours .

 foreach ($element->{'time-entry'} as $entry) { $description = (string) $entry->description; $hours = (string) $entry->hours; printf("Description: %s\nHours: %s\n\n", $description, $hours); } 

Outputs something like:

 Description: TCDM1 data management: sort & upload NFP SubProducers list Hours: 1.0 Description: PDCH1: HTML Hours: 0.25 

Note that the name of the time-entry element must be wrapped in curly braces 1 as a string, because otherwise $element->time-entry will try to read the time element and subtract the value of the constant called entry , which, obviously, is not what we want to. In addition, the description and hours are passed to lines 2 (they would otherwise be objects of type SimpleXMLElement ).

1. See variable variables 2. See type casting

+1
source share

Sorry I'm new to PHP. I was just starting to learn, so my concepts are taken from C programming. I could do an excessive amount of work / coding here ... but I'm sure I will scream to someone and find out.

This returns the array back, which is well laid out to access all individual bits of data without the need to process SimpleXMLElement objects:

 public $xmlFileInName = 'sample.xml'; public $errorMessage = "Failed to open file: "; public function actionGetArray() { try { $xml = file_exists($this->xmlFileInName) ? simplexml_load_file($this->xmlFileInName) : exit($this->errorMessage. $this->xmlFileInName); $xmlArray=$this->arrayFromSxe ($xml); echo var_dump($xmlArray); } catch(Exception $e) { echo $e->getMessage(); } } public function arrayFromSxe($sxe) { $returnArray = array(); foreach ((array)$sxe as $key=>$value) { if(is_array($value) && !$this->is_assoc($value)) { $indies = array(); foreach($value as $secondkey=>$secondvalue) $indies[$secondkey] = $this->arrayFromSxe($secondvalue); $returnArray[$key] = $indies; } else { if(is_object($value)) { $returnArray[$key] = $this->arrayFromSxe($value); } else { $returnArray[$key] = $value; } } } return $returnArray; } function is_assoc($array) { return (bool)count(array_filter(array_keys($array), 'is_string')); } 

Note that with multiple xml tags, i.e. in your example:

 [time-entry] => Array ( [0] => SimpleXMLElement Object ( ... ) [1] => SimpleXMLElement Object ( ... ) 

You will need to learn something about the structure of the XML file in order to access this in code. This will be returned to the array after calling this line:

 $xmlArray=$this->arrayFromSxe ($xml); 

What will happen:

 [time-entry][0] [time-entry][1] 

Thus, your code must know to iterate over all time entries via int indexers or in any situation in which they are multiple occurrences of an xml element.

If what I said does not make sense, do not pay attention to it and just observe and evaluate the code in its current form.

Hope this helps.

0
source share

All Articles