Sort all attributes in an XML tree using PHP XMLReader

I have PHP code to turn an XML file into a CSV file. During testing, I do not create a CSV file, I simply repeat the results in CSV format.

Whenever an XMLReader reaches an empty element, it displays all the attributes of the element.

1) Is there a way to display the attribute name with its values โ€‹โ€‹ie (is there a value of $ xml-> AttributeName that matches the value of $ xml->)?

2) Is there a sorting method for all attributes in the whole tree, and not just in an empty element?

<?php ini_set('memory_limit','50M'); $x = file_get_contents('H8_data.xml'); $xml = new XMLReader(); $xml->open('H8_data.xml', null, 1<<19); $num = 1; while ($xml->read() && $num <= 2000) { if($xml->isEmptyElement) { if($xml->hasAttributes) { while($xml->moveToNextAttribute()) { echo $xml->value, ', '; } } echo '<br />'; $num++; } } 

? >

+4
source share
2 answers

$ xml-> name returns the qualified name of the node. Since attributes are nodes of type XMLReader :: ATTRIBUTE, in this case the name $ xml-> will return the name of the current attribute. The following is a version of the code that displays the names and values โ€‹โ€‹of attributes.

 <?php ini_set('memory_limit','50M'); $x = file_get_contents('H8_data.xml'); $xml = new XMLReader(); $xml->open('H8_data.xml', null, 1<<19); $num = 1; while ($xml->read() && $num <= 2000) { if($xml->isEmptyElement) { if($xml->hasAttributes) { while($xml->moveToNextAttribute()) { echo $xml->name, ' = ', $xml->value, ', '; } } echo '<br />'; $num++; } } ?> 
+3
source

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


All Articles