I have some PHP code to turn an XML file into a CSV file. During testing, I am not creating a CSV file, just echoing the results in a CSV format.
Whenever XMLReader reaches an empty element, it outputs all the attributes of the element.
1) Is there a way to output the attribute name with it's values i.e. (is there an $xml->AttributeName that goes with the $xml->value)?
2) Is there a way to sort for all attributes in the entire tree and not just those in the empty element?
<?php开发者_如何学Python
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++;
}
}
?>
$xml->name returns the qualified name of the node. Because attributes are nodes with XMLReader::ATTRIBUTE type, $xml->name will return the name of current attribute in that case. Below is version of code, which outputs both attribute names and values.
<?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++;
}
}
?>
$xml->name ?
http://www.php.net/manual/en/class.xmlreader.php#xmlreader.props.name
精彩评论