88744544

Simple XML Parsing XML Attributes

I have an XML file with data stored like this:

<myxml> <item name="column18">88744544</item> <item name="column11">47884994</item> <item name="column3">44788894</item> </myxml> 

I need to first check (and make sure) that column11 is defined (there is no specific order), and then get its value.

Using plain XML does not work.

I have the following, but the value is missing.

 <?php if (count($xml->myxml->item) > 0) { foreach ($xml->myxml->item as $item) { var_dump($item->attributes()); } } ?> 

$ item-> attributes () → column11 does not work.

+4
source share
2 answers

Try XPath .

 if ($xml->xpath('//item[@name="column11"]')) { echo 'exists'; } 
+3
source

Do not include open tabs and attributes. For instance:

 <?php if (count($xml->item) > 0) { foreach ($xml->item as $item) { var_dump($item); //For the info echo $item['name']; //if you needed the name } } ?> 
+4
source

Source: https://habr.com/ru/post/1307499/


All Articles